reference USER CONTROLS label from aspx page...

  • Thread starter Thread starter Kruno
  • Start date Start date
K

Kruno

Hi !
if anyone knows the answer I would appreciate it:

I have a user control for the header of the page with
one label in it..I want the label to change as the pages
are changing....

my question:

I want to reference a text property of a label in the control
from the page which holds the control...
How can I do that ?
please, in code....
 
If i Understand you properly and you have a customer User control that has a Label that you wish to be able to set programmatically with the page then your label would need to be
<label runat="server" id="lblMyLabel">

If you are running VS.NET and you type the above code in your .ascx page click the "Design" button an dthen the "HTML" button and the next line of code will be added for you.
in your UserControl's CodeBehind you would have to make sure you had a defenition that went something like
protected System.Web.UI.HTML.HtmlGenericControl lblMyLabel;

Now the smartest thing to do would be to set a property accessor for it so in the UserControl's code behind also hadd
public string MyLabelText
{
get { return lblMyLabel.InnerText; }
set { lblMyLabel.InnerText = value; }
}

Then on your Page where you have something that looks like
<cc1:MyUserControl runat="server" id="uscMyUserControl">

On your Page's Code Behind you Must create a definition for it
protected MyUserControl uscMyUserControl; // Note this assumes MyUserControl is the class name and uscMyUserControl is the ID specified in your .aspx page

now in your PageLoad you should be able to go
uscMyUserControl.MyLabelText = "Hello";

And if you followed all of the instructions through properly when you run wherever your UserControl is a "hello" should appear on your page.

Hope this helps.
 
Back
Top