简体   繁体   English

我的查找控件未在gridview项模板中找到标签,有什么主意我该如何解决?

[英]My find control isn't finding a label in gridview item template, any ideas how can I fix this?

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. 我试图使用与我多次使用的相同技术将值附加到我的gridview的标签上,即使在此页面中也是如此,但是find控件无法找到该标签。 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. 从我完成的研究中,我遇到了一些情况,其中在一个项目模板中有2个标签会导致此问题,但在某些情况下却不会。

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;
  • The RowDataBound event will trigger on every rows in the grid. RowDataBound事件将在网格中的每一行上触发。
  • 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) if(e.Row.RowType == DataControlRowType.DataRow)

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

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM