繁体   English   中英

在asp.net中获取嵌套控件

[英]Get the nested controls in asp.net

我有一个家长用户控件,其中已经注册了一个孩子用户控件。 我想访问从母版页继承的aspx页中子usercontrol中存在的控件。

下面是我的代码:

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

当我这样做时,我的代码说线程中充满了一些代码……有人可以建议我做错了什么,对此应该采取什么解决方案。

我假设您正在输出窗口中谈论消息? (所以不是编译器错误还是运行时错误?)

在这种情况下:这是正常行为。 每次客户端请求页面时,都会启动一个线程,并且在呈现页面并将其发送回客户端时,该线程将终止产生此消息。 完全不用担心。

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

您后面的父控制代码将如下所示

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

暂无
暂无

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

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