Home All Groups Group Topic Archive Search About
Author
13 May 2005 7:24 PM
Paul
I declare a UserControl called header and in that header I declare a
<asp:label id=name> control. I reference this user control at the top of my
HTML page.

In my Page_load subroutine, I try to programatically set that "name" equal
to something.

I get an error saying that the "name" has not been defined.

My guess is that the page_load routine is being run before the UserControl
is referenced within the page.

Is there a way that I can do this without getting the error?

Author
14 May 2005 7:31 AM
Fred Hirschfeld
You need to create a Name property on your custom control that exposes the
label name...

public readonly Label Name {
    get { return this.name }
}

Fred

Show quoteHide quote
"Paul" <P***@discussions.microsoft.com> wrote in message
news:8A5E95B1-E263-4727-A657-0FDB37A4A938@microsoft.com...
>I declare a UserControl called header and in that header I declare a
> <asp:label id=name> control. I reference this user control at the top of
> my
> HTML page.
>
> In my Page_load subroutine, I try to programatically set that "name" equal
> to something.
>
> I get an error saying that the "name" has not been defined.
>
> My guess is that the page_load routine is being run before the UserControl
> is referenced within the page.
>
> Is there a way that I can do this without getting the error?
Author
15 May 2005 1:51 PM
Steve Goodyear
Hi Paul,

A UserControl will be a different class then the main page it's used on and
when you add a control to a user control it will usually have a protected
access level. In the user control add a property in the code-behind to expose
this property:

protected Label name;
....
public string LabelText
{
   get { return name.Text; }
   set { name.Text = value; }
}

And then you can reference the property:
<uc1:SampleUserControl LabelText="Here's some text" />

Cheers,
Steve Goodyear