2009-09-22 7 views
8

Mi dispiace, ma non riesco a capire perché questo non funziona. Dopo la compilazione, ricevo una "eccezione di riferimento Null". Per favore aiuto.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>"; 
    } 
} 

e interfaccia utente:

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

Dove viene visualizzata l'eccezione di riferimento null? – Joren

+0

Label1.Text = "Hai scelto " + DropDownList1.SelectedValue + " dal menu a discesa."; – AlexC

+0

Possibile duplicato http://stackoverflow.com/questions/799655/asp-net-findcontrol-is-not-working-come-come –

risposta

22

Per gentile concessione di Mr. Atwood himself, ecco una versione ricorsiva del metodo. Vorrei anche raccomandare test per null sul controllo e ho incluso come è possibile modificare il codice per farlo anche.

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

Grazie mille !!!!!!! – AlexC

+2

Buono per quando FindControl deve essere usato, ma nell'esempio di questa domanda FindControl è eccessivo. – CRice

2

FindControl ricerca solo nei figli immediate (tecnicamente al prossimo NamingContainer), non l'intera struttura di controllo. Poiché Label1 non è un figlio immediato di Master, Master.FindControl non lo localizzerà. Invece, vi sia bisogno di fare FindControl sul controllo padre immediato, o fare una ricerca di controllo ricorsiva:

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

(notare che questo è conveniente come extension method).

3

Quando Label1 esiste sulla pagina master:

Come raccontare la pagina di contenuti in cui la pagina master è

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

Poi facendo un metodo nel master come

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

E richiamarlo nel codice della pagina.

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

Quando Label1 esiste sulla pagina di contenuto

Se è semplicemente sulla stessa pagina, basta chiamare Label1.Text = someString; o se per qualche ragione devi usare FindControl, cambia il tuo Master.FindControl in FindControl

+0

+1, cancellata la mia risposta. Questo è un modo molto più semplice per realizzare ciò che vuoi. – Kelsey