2009-09-22 13 views
8

Üzgünüm, ama neden çalışmadığını anlayamıyorum. Derlendikten sonra "Boş başvuru istisnası" alıyorum. Lütfen yardım et.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>"; 
    } 
} 

ve 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

Boş başvuru istisnasını nereden alırsınız? – Joren

+0

Label1.Text = " seçtiniz" + DropDownList1.SelectedValue + " açılır menüden."; – AlexC

+0

Olası kopya http://stackoverflow.com/questions/799655/asp-net-findcontrol-is-not-working-how-come –

cevap

22
için

Mr. Atwood himself izninizi Master.FindControl değiştirin, bu yöntemin yinelemeli bir sürümü. Ayrıca, denetim için boş sınamanızı öneririm ve bunu yapmak için kodu nasıl değiştirebileceğinizi de ekledim.

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

Çok teşekkürler !!!!!!! – AlexC

+2

FindControl'un kullanılması gerektiğinde yararlıdır, ancak bu sorunun örneğinde FindControl fazladır. – CRice

2

FindControl sadece (teknik olarak bir sonraki NamingContainer kadar) hemen çocuklarda arar tamamını değil kontrol ağacı. Label1, Master numaralı bir çocuk olmadığından, Master.FindControl bulamaz. Bunun yerine, hemen üst kumanda üzerindeki FindControl yapın veya özyinelemeli kontrol araması yapmak gerekir ya:

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

(bu bir extension method olarak uygundur unutmayın).

3

Label1 ana sayfasında var olduğu zaman:

Nasıl

public void SetMessage(string message) 
{ 
    Label1.Text = message; 
} 
gibi usta bir yöntemi sizin ana sayfa olduğu

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

Ardından içerik sayfasını söylüyorum yapma konusunda

Ve sayfanın arkasındaki kod olarak adlandırın.

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

aynı sayfada basitçe ise Label1 içerik sayfasına

üzerinde var olduğunda, sadece Label1.Text = someString diyoruz; veya herhangi bir nedenle FindControl kullanmanız gerekiyorsa, FindControl

+0

+1, yanıtımı sildim. Bu, istediğini yerine getirmenin çok daha kolay bir yoludur. – Kelsey