2009-09-22 57 views
8

我很抱歉,但我不明白爲什麼這是行不通的。編譯後,我收到一個「空引用異常」。請幫忙。C#,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.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:

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

你從哪裏得到的空引用異常? – Joren 2009-09-22 00:29:59

+0

Label1.Text = 「你選擇」 + DropDownList1.SelectedValue + 「從下拉菜單中。」 – AlexC 2009-09-22 00:35:14

+0

可能的複製http://stackoverflow.com/questions/799655/asp-net-findcontrol-is-not-working-how-come – 2013-07-14 22:12:23

回答

22

致謝Mr. Atwood himself,這裏是遞歸版本的方法。我還建議在控制測試爲空,我包括如何更改代碼來做到這一點爲好。

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; 
} 
+0

非常感謝!!!!!!! – AlexC 2009-09-22 00:52:41

+2

適合當的FindControl需要使用的,但在這個問題上的例子FindControl已是矯枉過正。 – CRice 2009-09-22 00:53:39

2

FindControl只搜索在不久的孩子(技術上下一NamingContainer),而不是整個控件樹。由於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; 
} 

(請注意,這是方便爲extension method)。

3

當Label1的存在母版頁上:

如何講的內容頁面,您的主頁是

<%@ MasterType VirtualPath="~/MasterPages/PublicUI.Master" %> 

然後製作方法,在主像

public void SetMessage(string message) 
{ 
    Label1.Text = message; 
} 

並在後面的頁面代碼中調用它。

Master.SetMessage("<b>You chose <u>" + DropDownList1.SelectedValue + "</u> from the dropdown menu.</b>"); 

當Label1的內容頁

上存在如果它僅僅是在同一頁上,只需撥打Label1.Text = someString; 或者由於某種原因需要使用FindControl,請將您的Master.FindControl更改爲FindControl

+0

+1,刪除我的答案。這是一個更簡單的方法來完成你想要的。 – Kelsey 2009-09-22 00:59:18