开发者

Access property of parent page in user control

开发者 https://www.devze.com 2023-01-21 01:08 出处:网络
I have a hidden field on my default.aspx page. Within default.aspx page I have a user control which has a label on it. I need the label to display the value on the hidden field. How can I go ab开发者_

I have a hidden field on my default.aspx page. Within default.aspx page I have a user control which has a label on it. I need the label to display the value on the hidden field. How can I go ab开发者_StackOverflow社区out this?


Create a public property on the user control which exposes the label text:

public string LabelText
{
   get { return this.label1.Text; }
   set { this.label1.Text = value; }
}

Then set this from the page code-behind. So if the ASPX for your page has something like this in it:

<uc1:UserControl ID="userControl" runat="server" />

In the code-behind, you could do

this.userControl.LabelText = "something";

Doing it this way means that your user control doesn't have to know about the page that's using it. This helps the user control to be re-used on many different pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消