0

我有一个父用户控件,我在其中注册了一个子用户控件。我想访问我从母版页继承的 aspx 页面中的子用户控件中存在的控件。

下面是我的代码:

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

当我这样做时,我的代码说线程对一些代码感到兴奋......任何人都可以建议我我做错了什么以及应该是什么解决方案......谢谢

4

2 回答 2

0

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

在这种情况下:这是正常行为。每次客户端请求页面时,都会启动一个线程,当页面被渲染并发送回客户端时,该线程将终止生成此消息。没什么好担心的。

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

于 2013-04-28T00:22:12.993 回答
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>
于 2013-04-28T07:17:17.343 回答