Jay
Jay

Reputation: 539

How to initialize a ASP.NET User Control With parameter?

For example, I have a user control(ascx) with a label inside, I will use the the user control in my aspx page.

How can I pass a string value to the ascx page so that it can be display in the label of ascx page at the beginning?

Upvotes: 3

Views: 3310

Answers (2)

Kelsey
Kelsey

Reputation: 47726

You can expose whatever controls you want access to in your user control by creating property for them.

In the past when I have had user controls that required certain data for setup I would create an Initialize method which would take in and setup whatever was needed.

Upvotes: 1

David McEwing
David McEwing

Reputation: 3340

Add this...

public string Whatever
{
get { return label.Text; }
set { label.Text = value; }
}

to your ascx control. Then from the page you are putting it in you can just set the text like... usercontrol.Whatever = "text to display";

or you can use the Whatever as a property on the aspx side of the page.

Upvotes: 6

Related Questions