首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#,FindControl

C#,FindControl
EN

Stack Overflow用户
提问于 2009-09-22 00:27:57
回答 3查看 29.3K关注 0票数 9

我很抱歉,但我不明白为什么这个不起作用。编译后,我收到一个“空引用异常”。请帮帮忙。

代码语言:javascript
复制
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>";
    }
}

和UI:

代码语言:javascript
复制
<%@ 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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-09-22 00:50:17

感谢Mr. Atwood himself,下面是该方法的递归版本。我还建议在控件上测试null,我还介绍了如何更改代码来实现这一点。

代码语言:javascript
复制
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;
}
票数 22
EN

Stack Overflow用户

发布于 2009-09-22 00:38:40

当Label1存在于母版页上时为

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

代码语言:javascript
复制
<%@ MasterType VirtualPath="~/MasterPages/PublicUI.Master" %>

然后在master中创建一个方法,如下所示

代码语言:javascript
复制
public void SetMessage(string message)
{
    Label1.Text = message;
}

并在page的代码中调用它。

代码语言:javascript
复制
Master.SetMessage("<b>You chose <u>" + DropDownList1.SelectedValue + "</u> from the dropdown menu.</b>");

当Label1存在于内容页上时为

如果只是在同一页上,只需调用Label1.Text = someString;或者,如果出于某种原因需要使用FindControl,请将Master.FindControl更改为FindControl

票数 4
EN

Stack Overflow用户

发布于 2009-09-22 00:32:59

FindControl只搜索直接的子级(从技术上讲,搜索到下一个NamingContainer),而不是整个控件树。因为Label1不是Master的直接子对象,所以Master.FindControl不会定位它。相反,您需要在直接父控件上执行FindControl,或者执行递归控件搜索:

代码语言:javascript
复制
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;
}

(请注意,作为extension method,这很方便)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1457567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档