2009-09-22 6 views
8

Przykro mi, ale nie rozumiem, dlaczego to nie działa. Po kompilacji otrzymuję "wyjątek odwołania zerowego". Proszę pomóż.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>"; 
    } 
} 

i 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

Gdzie otrzymasz wyjątek odwołania zerowego? – Joren

+0

Label1.Text = "Wybrali Państwo " + DropDownList1.SelectedValue + " z menu rozwijanego"; – AlexC

+0

Możliwe duplikaty http://stackoverflow.com/questions/799655/asp-net-findcontrol-is-not-working-how-come –

Odpowiedz

22

Dzięki uprzejmości Mr. Atwood himself, oto rekursywna wersja metody. Poleciłbym również testowanie wartości NULL na kontrolerze i dodałem, w jaki sposób można również zmienić kod, aby to zrobić.

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

Wielkie dzięki !!!!!!! – AlexC

+2

Dobre rozwiązanie, gdy trzeba użyć FindControl, ale w tym pytaniu przykład FindControl jest przesadny. – CRice

2

FindControl przeszukuje tylko w bezpośrednim dzieci (technicznie do następnego NamingContainer), a nie całe drzewo kontrola. Ponieważ Label1 nie jest bezpośrednim potomkiem Master, Master.FindControl go nie zlokalizuje. Zamiast tego, to albo trzeba zrobić FindControl na bezpośredniej kontroli nadrzędnej, czy rekurencyjne przeszukiwanie sterowania:

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

(Uwaga ta jest wygodna jako extension method).

3

Kiedy Label1 istnieje na stronie głównej:

Jak o mówienie zawartości strony, gdzie strona wzorcowa jest

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

Następnie czyni metodę w pana jak

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

I wywołaj go w kodzie strony.

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

Kiedy Label1 istnieje na stronie zawartości

Jeśli jest to po prostu na tej samej stronie, po prostu zadzwoń Label1.Text = someString; lub jeśli z jakiegoś powodu potrzebujesz użyć FindControl, zmień Master.FindControl na FindControl

+0

+1, usunąłem odpowiedź. Jest to znacznie łatwiejszy sposób na osiągnięcie tego, co chcesz. – Kelsey