获取asp.net中的嵌套控件
本文关键字:嵌套 控件 asp net 获取 | 更新日期: 2023-09-27 18:11:14
我有一个父用户控件,我在其中注册了一个子用户控件。我想访问从母版页继承的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>
当我这样做的时候,我的代码说线程兴奋与一些代码…谁能告诉我我做错了什么,应该怎么解决这个问题…由于
我假设您在输出窗口中谈论消息?(所以不是编译错误或运行时错误?)
在这种情况下,这是正常的行为。每次客户端对页面进行请求时,都会启动一个线程,当页面呈现并发送回客户端时,该线程将终止并产生此消息。没什么好担心的。
参见: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!";
}
}
//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" %>
<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
<%@ 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>