首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

如何獲取 GridView 的 EmptyDataTemplate 中的控件

     編輯:關於ASP.NET

問題:Controls[0].Controls[0] 易出 Null 異常

網上常見寫法在有資料時可能因索引越界拋出異常。

推薦做法:RowCreated 事件 + FindControl

protected void GridViewSearchResult_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.EmptyDataRow)
    {
        ((Label)(e.Row.FindControl("LabelKeyword"))).Text = Request["keyword"];
    }
}

FindControl 直接依 ID 尋找,型別轉換後即可操作,簡潔安全,不依賴控件集合索引順序。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved