更新时间:2023-12-06 10:49:16
您需要公开的公共属性和用户控件的控件的父页面的构造函数。
You need to expose public properties and a constructor of the UserControl's Controls to the parent page.
假设你的用户控件得到了一个标签:
Say your Usercontrol has got a label:
<asp:Label ID="MyLabel" runat="server" visible="true"/>
在用户控件的codebehind添加此。
In the codebehind of the UserControl add this.
//Constructor
public MyUserControl()
{
Category = new Label();
}
//Exposing the Label
public Label Category
{
get { return this.MyLabel; }
set { this.MyLabel = value; }
}
假设你已经添加了用户控件到父页面,它的ID是的MyUserControl。
Assume you have added the UserControl to the parent page and its ID is "MyUserControl".
要在用户控件的标签值设置为使用的东西这样的:
To set the label value of the UserControl to something use this:
MyUserControl.Category.Text=Response.QueryString["categoryname"];//Obviously you would want to encode it first.
如果您需要调用在该用户的codebehind父页面的功能,你将不得不使用委托。我不会推荐,虽然这个方法。
If you need to call functions of the parent page in the UserControl's codebehind, you will have to use delegates. I would not recommend this method though.