網上常見寫法在有資料時可能因索引越界拋出異常。
protected void GridViewSearchResult_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.EmptyDataRow)
{
((Label)(e.Row.FindControl("LabelKeyword"))).Text = Request["keyword"];
}
}
FindControl 直接依 ID 尋找,型別轉換後即可操作,簡潔安全,不依賴控件集合索引順序。