Churchill
Churchill

Reputation: 1607

Access property of parent page in user control

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 about this?

Upvotes: 0

Views: 2543

Answers (1)

Graham Clark
Graham Clark

Reputation: 12956

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.

Upvotes: 1

Related Questions