Domanda

    

Questa domanda ha già una risposta qui:

         

Mi dispiace, ma non riesco a capire il motivo per cui questo non funziona. Dopo la compilazione, ricevo un "eccezione di riferimento Null". Si prega di aiutare.

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>
È stato utile?

Soluzione

Per gentile concessione di Mr. Atwood se stesso , ecco una versione ricorsiva del metodo. Consiglio anche di prova per nulla sul controllo e ho compreso come si può cambiare il codice per fare anche questo.

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

Altri suggerimenti

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 lo chiamano nel codice della pagina di dietro.

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

Quando Label1 esiste sulla pagina di contenuti

Se si tratta semplicemente sulla stessa pagina, basta chiamare Label1.Text = someString; o se per qualche motivo è necessario utilizzare FindControl, modificare la Master.FindControl a FindControl

FindControl ricerca solo nei figli immediate (tecnicamente alla prossima NamingContainer ), non l'intera struttura di controllo. Dal momento che Label1 non è un figlio diretto di Master, Master.FindControl non localizzarlo. 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;
}

(Nota: questo è conveniente come un rel metodo di estensione ).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top