How to get/set property in User Controls
how can i set the property for user/Custom controls
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
arun maheshwariPosted Feb 22, 2008, 11:14 PM
n ur way of explanation is good.
NainilPosted Feb 22, 2008, 9:14 PM
Hi Arun,
Setting properties for controls within UserControl is very simple. Let me explain this using an example. Let's say you have a UserControl called UserControl1.ascx. You place a TextBox (with ID TextBox1) in the UserControl1.ascx page, and you want to use a proeprty called TextBoxText to get and set Text on TextBox control. For this scenario, you will then create a property called TextBoxText which gets (returns) TextBox1.Text. You will then set the value to TextBox1.Text property. Following is the code snippet as mentioned above to get and set property to retrieve and set string to the TextBox contorl on the UserControl1.ascx page:
public string TextBoxText{
get { return this.TextBox1.Text; } set { this.TextBox1.Text = value; }}
Then you can drag and drop the UserControl1.ascx to any webpage and access the property TextBoxText from the UserControl just like any other normal control. So for example after dropping the UserControl1 to a web page, you can access the TextBoxText property as follows:
string textFromUserControl = this.UserControl1.TextBoxText; // Gets the text
this.UserControl1.TextBoxText = "Hello World!"; // Sets the text to the TextBox in the UserControl
Please feel free to write back if you need any further calrification. Thanks.