C#的FindControlFindControl

2023-09-02 01:33:52 作者:零乱的悲情在雨季重演

我很抱歉,但我不明白为什么这是行不通的。编译后,我收到一个空引用异常。请大家帮帮忙。

 公共部分类labs_test:System.Web.UI.Page
{
    保护无效的button1_Click(对象发件人,EventArgs的)
    {
        如果(TextBox1.Text!=)
        {
            标签Label1 =(标签)Master.FindControl(标签1);
            Label1.Text =< B>您输入的文字是:+ TextBox1.Text +< / B>中;
        }
    }

    保护无效DropDownList1_SelectedIndexChanged(对象发件人,EventArgs的)
    {
        标签Label1 =(标签)Master.FindControl(标签1);
        Label1.Text =< B>您选择了< U>中+ DropDownList1.SelectedValue +< / U>从下拉菜单< / B>中;
    }
}
 

和用户界面:

 <%@页面语言=C#的MasterPageFile =〜/ MasterPage.masterAutoEventWireup =真正的codeFILE =test.aspx.cs继承= labs_test标题=无标题页%>

< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
键入文本,然后单击按钮,在一个标签是在母版显示文字< BR />
这是使用的FindControl< BR />
< ASP:文本框ID =TextBox1的=服务器>< / ASP:文本框>
< ASP:按钮的ID =Button1的=服务器的OnClick =的button1_Click文本=提交/>< BR />
< BR />
从下面的列表中选择一个项目,它会显示在作为标签
在母版< BR />
这是使用的FindControl< BR />
< ASP:DropDownList的ID =DropDownList1=服务器的AutoPostBack =真OnSelectedIndexChanged =DropDownList1_SelectedIndexChanged>
< ASP:列表项>项目1< / ASP:列表项>
< ASP:列表项>项目2'; / ASP:列表项>
< ASP:列表项>项目第3版; / ASP:列表项>
< / ASP:DropDownList的>
< ASP:标签ID =标签1=服务器文本=标签>< / ASP:标签>
< / ASP:内容>
 

解决方案

礼貌先生阿特伍德本人,这里的方法的递归版本。我还建议测试空的控制和I包括了如何改变code做到这一点。

 保护无效的button1_Click(对象发件人,EventArgs的)
{
    如果(TextBox1.Text!=)
    {
        标签Label1 = FindControlRecursive(页,标签1)作为标签;
        如果(标签1!= NULL)
            Label1.Text =< B>您输入的文字是:+ TextBox1.Text +< / B>中;
    }
}

保护无效DropDownList1_SelectedIndexChanged(对象发件人,EventArgs的)
{
    标签Label1 = FindControlRecursive(页,标签1)作为标签;
    如果(标签1!= NULL)
        Label1.Text =< B>您选择了< U>中+ DropDownList1.SelectedValue +< / U>从下拉菜单< / B>中;
}

私人控制FindControlRecursive(控制根弦ID)
{
    如果(root.ID == ID)返回根;
    的foreach(在root.Controls控制C)
    {
        控制T = FindControlRecursive(C,ID);
        如果(T!= NULL)返回吨;
    }
    返回null;
}
 
细说FindControl

I'm sorry, but I can't understand why this doesn't work. After compile, I receive a "Null reference exception". Please help.

public partial class labs_test : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Label Label1 = (Label)Master.FindControl("Label1");
            Label1.Text = "<b>The text you entered was: " + TextBox1.Text + ".</b>";
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label Label1 = (Label)Master.FindControl("Label1");
        Label1.Text = "<b>You chose <u>" + DropDownList1.SelectedValue + "</u> from the dropdown menu.</b>";
    }
}

and UI:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="labs_test" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Type in text and then click button to display text in a Label that is in the MasterPage.<br />
This is done using FindControl.<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /><br />
<br />
Choose an item from the below list and it will be displayed in the Label that is
in the MasterPage.<br />
This is done using FindControl.<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    
</asp:Content>

解决方案

Courtesy of Mr. Atwood himself, here's a recursive version of the method. I would also recommend testing for null on the control and I included how you can change the code to do that as well.

protected void Button1_Click(object sender, EventArgs e)
{
    if (TextBox1.Text != "")
    {
        Label Label1 = FindControlRecursive(Page, "Label1") as Label;
        if(Label1 != null)
            Label1.Text = "<b>The text you entered was: " + TextBox1.Text + ".</b>";
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label Label1 = FindControlRecursive(Page, "Label1") as Label;
    if (Label1 != null)
        Label1.Text = "<b>You chose <u>" + DropDownList1.SelectedValue + "</u> from the dropdown menu.</b>";
}

private Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id) return root;
    foreach (Control c in root.Controls)
    {
        Control t = FindControlRecursive(c, id);
        if (t != null) return t;
    }
    return null;
}

相关推荐