简体   繁体   English

在asp.net中获取嵌套控件

[英]Get the nested controls in asp.net

I have a Parent User Control in which i have registered a Child UserControl. 我有一个家长用户控件,其中已经注册了一个孩子用户控件。 I want to access the controls present in the child usercontrol in my aspx page that I have inherited from a Master page. 我想访问从母版页继承的aspx页中子usercontrol中存在的控件。

Below is my 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>

When i do this, my code says thread excited with some code... Can anybody suggest me what i`m doing wrong and what should be the solution for this.. Thanks 当我这样做时,我的代码说线程中充满了一些代码……有人可以建议我做错了什么,对此应该采取什么解决方案。

I assume you are talking messages in your output-window? 我假设您正在输出窗口中谈论消息? (So not a compiler error or runtime error?) (所以不是编译器错误还是运行时错误?)

In that case: that is normal behavior. 在这种情况下:这是正常行为。 Every time a client does a request for a page, a thread is started and when the page is rendered and sent back to the client, that thread will terminate an producte this message. 每次客户端请求页面时,都会启动一个线程,并且在呈现页面并将其发送回客户端时,该线程将终止产生此消息。 Nothing to worry about. 完全不用担心。

See also: http://msdn.microsoft.com/en-us/library/bs4c1wda.aspx 另请参阅: http : //msdn.microsoft.com/en-us/library/bs4c1wda.aspx

your parent control code behind will be like this 您后面的父控制代码将如下所示

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

    }
}

child control codebehind will be 子控制代码后面将是

 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 page codebehind will be 后面的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!";
    }
}

//My Parent usercontrol .ascx stuff //我的父用户控件.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" />

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM