tags:

views:

26

answers:

1

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?

+1  A: 

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.

Graham Clark
Thanks. How would I use this on a master page formation? Can I create a class that changes a Div content using this method? HtmlGenericControl name = (HtmlGenericControl)Master.FindControl("name"); does not show anything on the class section.
Kenyana
How do I acccess LabelText from code behind?
Kenyana
@Kenyana: I'm not quite sure what you mean by "master page formation". You would probably be better using a `Panel` rather than a div. I'll update my answer a bit...
Graham Clark