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.
精彩评论