Reputation: 12755
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 either be there or not? Any ideas?
The label is defined like this:
<asp:Label ID="LBL_MyLabel" runat="server"></asp:Label>
Thanks :-)
Upvotes: 0
Views: 1429
Reputation: 218828
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.
Upvotes: 1