9

申し訳ありませんが、なぜこれが機能しないのか理解できません。コンパイル後、「Null 参照例外」が発生します。助けてください。

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>
4

3 に答える 3

22

Atwood氏自身の厚意により、メソッドの再帰バージョンを次に示します。また、コントロールで null をテストすることをお勧めします。コードを変更してそれを行う方法も含めました。

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;
}
于 2009-09-22T00:50:17.077 に答える
4

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 に変更します。

于 2009-09-22T00:38:39.970 に答える
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;
}

(これは拡張メソッドとして便利であることに注意してください)。

于 2009-09-22T00:32:58.593 に答える