I am trying to attach a value to a label on my gridview using the same technique that I have used many times, even in this very page, but the find control isn't finding the label. Does anyone know why this might be? From the research that I've done I've come across some instances where having 2 labels in one item template causes this problem but in some cases, it doesn't.
Gridview:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStockDetailsS" runat="server"></asp:Label>
<asp:Label ID="lbl7" runat="server" Text="hello"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
Code behind:
Label lblSD = (Label)e.Row.FindControl("lblStockDetailsS");
lblSD.Text = Sline.StockDetailsS;
Label lblSD2 = (Label)e.Row.FindControl("lbl7");
lblSD2.Text = Sline.NLocalStock;
Have your code block inside this condition.
if(e.Row.RowType == DataControlRowType.DataRow)
Ref: https://techpattarai.com/findcontrol-onrowdatabound-csharp/
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.