0

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;
  • Where are you calling `e.Row.FindControl("lbl7")`? – VDWWD Feb 05 '19 at 09:47
  • @VDWWD It's in the RowDataBound event for that grid – Andy Douthwaite Feb 05 '19 at 09:49
  • Forgive me if you tried this suggestion already, but have you removed lblStockDetails to check if lbl7 would then work? – Doug F Feb 05 '19 at 13:33
  • @DougF That is a good suggestion, I did try it earlier but it broke another part of the app so I couldn't get to this point to see if affected this bit. – Andy Douthwaite Feb 05 '19 at 13:54
  • Hmm. Does it have to be a label? Can you use something else, like a literal? I know it's not the same thing, but I'm just curious if that would work. Or maybe do a search for "lbl7" to see if there's any code somewhere that might be affecting it? – Doug F Feb 05 '19 at 14:04
  • @DougF I have already done a search for lbl7 and it doesn't appear anywhere else. I haven't tried using a text literal yet though. Perhaps I could try one label and one text literal. I'll let you know how it goes! – Andy Douthwaite Feb 05 '19 at 15:44

1 Answers1

2
  • The RowDataBound event will trigger on every rows in the grid.
  • You have check whether the current row is header or data row before finding the controls which is available in the data row.
  • Have your code block inside this condition.

    if(e.Row.RowType == DataControlRowType.DataRow)

Ref: https://techpattarai.com/findcontrol-onrowdatabound-csharp/

Thanks

Rajesh
  • 19
  • 1
  • Thanks Rajesh, I do already have this in my code. I have two labels in the rowdatabound one works and one doesn't. it's quite a mystery! – Andy Douthwaite Feb 07 '19 at 08:58
  • Have you used both labels in the same place? If so can you post the html of both the labels. So that I can try to sort this out. Thanks – Rajesh Feb 07 '19 at 09:07
  • I've done it all from the code behind but I have updated my question to include the other label. Thanks – Andy Douthwaite Feb 07 '19 at 09:21
  • I'm not sure it is typo. Just check the label ID with an ID you used in the code behind. The code you updated now has one more s int it lblStockDetailsS. – Rajesh Feb 07 '19 at 09:27
  • You're quite right, just a typo though, it is correct in my code, well spotted! – Andy Douthwaite Feb 07 '19 at 09:32