User Controls

P

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?
 
F

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
 
S

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top