Groups | Blog | Home
all groups > asp.net webcontrols > may 2005 >

asp.net webcontrols : User Controls


Paul
5/13/2005 12:24:03 PM
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.

Fred Hirschfeld
5/14/2005 12:31:53 AM
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

[quoted text, click to view]

Steve Goodyear
5/15/2005 6:51:02 AM
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,
AddThis Social Bookmark Button