简体   繁体   English

命名容器中的findControl(Asp.net Webform C#)

[英]findControl in Naming container (Asp.net webform C#)

I have a problem about findControl method in naming container. 我在命名容器中遇到有关findControl方法的问题。 It's not the first trouble about that and I would like understand the theory. 这不是第一个麻烦,我想了解一下理论。 I found many solutions on website but nothing works 我在网站上找到了许多解决方案,但没有任何效果

I have a DetailsView which contains controls. 我有一个包含控件的DetailsView。 I put DefaultMode "Insert" and I add 2 radio buttons 我将DefaultMode“插入”,然后添加2个单选按钮

<asp:DetailsView ID="DetailsView1" runat="server"
        ItemType="[...]"
        DefaultMode="Insert"
        [...]">
        <Fields>
            <asp:TemplateField>
                <InsertItemTemplate>
                    <asp:Panel ID="Panel1" runat="server" GroupingText="Create or Select">
                        <div class="Select">
                            <asp:RadioButton ID="RB_Select" runat="server" Text="Select" Checked="True" AutoPostBack="true" OnCheckedChanged ="RB_Select_CheckedChanged" />
                            <asp:DropDownList runat="server" ID="DDL_Select"
                                ItemType="[...]"
                                [...]
                                AutoPostBack="true">
                            </asp:DropDownList>
                        </div>
                        <div class="New">
                            <asp:RadioButton ID="RB_New" runat="server" Text="New" Checked="false" AutoPostBack="true" OnCheckedChanged="RB_New_CheckedChanged" />
                            <asp:TextBox ID="TXB_New" runat="server" Enabled="false" Text="<%# BindItem.Label %>"></asp:TextBox>
                        </div>
                    </asp:Panel>
                </InsertItemTemplate>
          </asp:TemplateField>
      </Fields>
</asp:DetailsView>

And for exemple in my behind Code, I Just want to test if radiobutton is check or not : 例如,在我的背后代码中,我只想测试单选按钮是否被选中:

protected void RB_New_CheckedChanged(object sender, EventArgs e)
{
        var RadioButtonNew = (RadioButton)FindControl("RB_New");
        var RadioButtonSelect = (RadioButton)FindControl("RB_Select");

        RadioButtonSelect.Checked = !RadioButtonNew.Checked;
}

And I have a "System.NullReferenceException" because it doesn't find my controls. 我有一个“ System.NullReferenceException”,因为它找不到我的控件。

Why it doesn't recognize my controls? 为什么无法识别我的控件? And how to deal with this? 以及如何处理呢?

Thanks in advance 提前致谢

You are using FindControl on a Page level. 您正在页面级别上使用FindControl But the Controls are inside a DetailsView, so you need to access that first. 但是控件位于DetailsView内部,因此您需要首先访问它。

TextBox tb = DetailsView1.FindControl("TXB_New") as TextBox;

//or

var RadioButtonSelect = (RadioButton)DetailsView1.FindControl("RB_Select");

Thank you again, I found the solution. 再次感谢您,我找到了解决方案。

I didn't know but Panel element acted like a container. 我不知道,但是Panel元素就像一个容器。 I just add a findControl : 我只是添加一个findControl:

var RadioButtonSelect = (RadioButton)DetailsView1.FindControl("Panel1").FindControl("RB_Select");

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

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