开发者

DataGridItem.FindControl doesnt find the control? (ASP.NET)

开发者 https://www.devze.com 2023-02-22 04:59 出处:网络
Label lbl = dgi.FindControl(\"LBL_MyLabel\") as Label; This works most of the time, but sometimes lbl is null after FindControl was called. I am wondering how this could happen. It should eit开发者
Label lbl = dgi.FindControl("LBL_MyLabel") as Label; 

This works most of the time, but sometimes lbl is null after FindControl was called. I am wondering how this could happen. It should eit开发者_运维百科her be there or not? Any ideas?

The label is defined like this:

<asp:Label ID="LBL_MyLabel" runat="server"></asp:Label>

Thanks :-)


What's the broader context of the code around the call to FindControl? This error is commonly experienced when iterating through the rows in the grid (such as in the RowDataBound event) without conditionally checking what the row type is:

if (e.row.RowType == DataControlRowType.DataRow)
{
  // your code
}

Wrapping it in that conditional will skip header/footer rows, which probably don't have your label control in them.

0

精彩评论

暂无评论...
验证码 换一张
取 消