C#,FindControl

C#,FindControl,c#,.net,asp.net,findcontrol,C#,.net,Asp.net,Findcontrol,对不起,我不明白为什么这样不行。编译后,我收到一个“空引用异常”。请帮忙 public partial class labs_test : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text != "") { Label Label1 = (Label)Master.FindCont

对不起,我不明白为什么这样不行。编译后,我收到一个“空引用异常”。请帮忙

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>";
    }
}
公共部分类实验室\u测试:System.Web.UI.Page
{
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
如果(TextBox1.Text!=“”)
{
Label1=(Label)Master.FindControl(“Label1”);
Label1.Text=“您输入的文本是:“+TextBox1.Text+”;
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
Label1=(Label)Master.FindControl(“Label1”);
Label1.Text=“您从下拉菜单中选择了“+DropDownList1.SelectedValue+”;
}
}
和用户界面:

<%@ 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>

键入文本,然后单击按钮以母版页中的标签显示文本。
这是使用FindControl完成的。


从下面的列表中选择一个项目,它将显示在显示的标签中 在母版页中。
这是使用FindControl完成的。
项目1 项目2 项目3
FindControl
只搜索直接子项(技术上是下一个),而不是整个控件树。由于
Label1
不是
Master
的直接子级,
Master.FindControl
将找不到它。相反,您需要对直接父控件执行
FindControl
,或者执行递归控件搜索:

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

(注意,这是一种方便的方法)。

当母版页上存在Label1时:

告诉内容页母版页在哪里如何

<%@ MasterType VirtualPath="~/MasterPages/PublicUI.Master" %>
并在page的代码隐藏中调用它

Master.SetMessage("<b>You chose <u>" + DropDownList1.SelectedValue + "</u> from the dropdown menu.</b>");
Master.SetMessage(“您从下拉菜单中选择了“+DropDownList1.SelectedValue+”);

当内容页上存在标签1时

如果它只是在同一页上,只需调用Label1.Text=someString; 或者,如果出于某种原因需要使用FindControl,请将Master.FindControl更改为FindControl,这是该方法的递归版本。我还建议在控件上测试null,并介绍了如何更改代码来实现这一点

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;
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
如果(TextBox1.Text!=“”)
{
Label Label1=FindControlRecursive(第页,“Label1”)作为标签;
if(Label1!=null)
Label1.Text=“您输入的文本是:“+TextBox1.Text+”;
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
Label Label1=FindControlRecursive(第页,“Label1”)作为标签;
if(Label1!=null)
Label1.Text=“您从下拉菜单中选择了“+DropDownList1.SelectedValue+”;
}
专用控件FindControlRecursive(控件根,字符串id)
{
if(root.ID==ID)返回root;
foreach(root.Controls中的控件c)
{
控件t=FindControlRecursive(c,id);
如果(t!=null)返回t;
}
返回null;
}

从何处获取空引用异常?Label1.Text=“您从下拉菜单中选择了“+DropDownList1.SelectedValue+”;适用于需要使用FindControl的情况,但在本问题的示例中,FindControl是一种过激行为。+1删除了我的答案。这是一个更容易实现你想要的方法。
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;
}