2013-04-27 5 views
0

J'ai un contrôle utilisateur parent dans lequel j'ai enregistré un UserControl. Je veux accéder aux contrôles présents dans le contrôle utilisateur enfant dans ma page aspx dont j'ai hérité d'une page maître.Récupère les contrôles imbriqués dans asp.net

Ci-dessous mon code:

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

Quand je fais cela, mon code dit enfilez excité avec un code ... Quelqu'un peut-il me suggérer ce que i`m faire le mal et ce qui devrait être la solution pour cette .. Merci

Répondre

0

Je suppose que vous parlez des messages dans votre fenêtre de sortie? (Donc, pas une erreur de compilation ou une erreur d'exécution?)

Dans ce cas: c'est un comportement normal. Chaque fois qu'un client fait une demande pour une page, un thread est démarré et quand la page est rendue et renvoyée au client, ce thread se terminera et produira ce message. Pas d'inquiétudes à avoir.

Voir aussi: http://msdn.microsoft.com/en-us/library/bs4c1wda.aspx

0

votre code de contrôle parent derrière sera comme celui-ci

//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) 
    { 

    } 
} 

codebehind de contrôle des enfants sera

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) 
    { 

    } 
} 

page ASPX codebehind sera

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

// Mon stuff Parent usercontrol de

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

// Mon enfant 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> 

// Ma page ASPX 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="ctrlPageControl" runat="server" /> 
    </asp:Content> 
Questions connexes