2013-04-27 144 views
0

我有一個父用戶控件,其中我註冊了一個Child UserControl。 我想要訪問我從母版頁繼承的aspx頁面中的child usercontrol中存在的控件。獲取asp.net中的嵌套控件

下面是我的代碼:

//Parent UserControl 
    public partial class WebUserControlParent : System.Web.UI.UserControl 
    { 
     public WebUserControlChild checkbox 
     { 
      get 
      { 
       return this.checkbox; 
      } 
     } 
     public WebUserControlChild label 
     { 
      set 
      { 
       this.label = value; 
      } 
      get 
      { 
       return this.label; 
      } 
     } 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 
    } 
//Child User Control : 
    public partial class WebUserControlChild : System.Web.UI.UserControl 
    { 
     public bool Checked 
     { 
      set 
      { 
       this.checkboxchild.Checked = value; 
      } 
     } 
     public string Text 
     { 
      set 
      { 
       this.labelchild.Text = "YooHoo!"; 
      } 
     } 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 
    } 
//My Aspx Page: 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      this.PageControl.checkbox.Checked = true; 
      this.PageControl.label.Text = "YoooHooo!"; 
     } 
    } 
//My Parent usercontrol .ascx stuff 
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControlParent.ascx.cs" 
    Inherits="WebApplication2.WebUserControlParent" %> 
<%@ Register Src="~/WebUserControlChild.ascx" TagName="Child" TagPrefix="cc" %> 

//My Child Usercontrol Stuff 
     <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControlChild.ascx.cs" 
     Inherits="WebApplication2.WebUserControlChild" %> 
    <asp:CheckBox ID="checkboxchild" runat="server" Checked="false" /> 
    <asp:Label ID="labelchild" runat="server"></asp:Label> 

//My ASPX Page Stuff 
    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> 

    <%@ Register Src="~/WebUserControlParent.ascx" TagName="Control" TagPrefix="cc" %> 
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
    </asp:Content> 
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
     <cc:Control ID="PageControl" runat="server" /> 
    </asp:Content> 

當我這樣做,我的代碼表示線程高興能與一些代碼...任何人都可以建議我什麼我真的做錯了,應該是什麼這樣的解決方案..謝謝

回答

0

我假設你正在輸出窗口中的消息? (所以不是編譯器錯誤或運行時錯誤?)

在這種情況下:這是正常的行爲。每當客戶端請求一個頁面時,就會啓動一個線程,並在頁面呈現併發送回客戶端時,該線程將終止產生此消息。完全不用擔心。

參見:http://msdn.microsoft.com/en-us/library/bs4c1wda.aspx

0

你的父母控制代碼背後會是這樣

//Parent UserControl 
public partial class WebUserControlParent : System.Web.UI.UserControl 
{ 
    public WebUserControlChild mChildControl 
    { 
     get 
     { 
      return this.ctrlChild; 
     } 
     set{ 
      this.ctrlChild = value; 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

子控件的代碼隱藏將是

public partial class WebUserControlChild : System.Web.UI.UserControl 
{ 
    public bool Checked 
    { 
     set 
     { 
      this.checkboxchild.Checked = value; 
     } 
     get{ 
      return this.checkboxchild.Checked; 
     } 

    } 
    public string Text 
    { 
     set 
     { 
      this.labelchild.Text = value; 
     } 
     get{ 
      return this.labelchild.Text; 
     } 
    } 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

aspx頁面的代碼隱藏將是

public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     this.ctrlPageControl.mChildControl.Checked = true; 
     this.ctrlPageControl.mChildControl.Text = "YoooHooo!"; 
    } 
} 

//我父母用戶控件的.ascx東西

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControlParent.ascx.cs" 
    Inherits="WebApplication2.WebUserControlParent" %> 
    <%@ Register Src="~/WebUserControlChild.ascx" TagName="Child" TagPrefix="cc" %> 
    <cc:Control ID="ctrlChild" runat="server" /> 

//我的孩子用戶控件的東西

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControlChild.ascx.cs" 
    Inherits="WebApplication2.WebUserControlChild" %> 
    <asp:CheckBox ID="checkboxchild" runat="server" Checked="false" /> 
    <asp:Label ID="labelchild" runat="server"></asp:Label> 

//我的ASPX頁面的東西

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> 

    <%@ Register Src="~/WebUserControlParent.ascx" TagName="Control" TagPrefix="cc" %> 
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
    </asp:Content> 
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <cc:Control ID="ctrlPageControl" runat="server" /> 
    </asp:Content>