2
Answers

How to get/set property in User Controls

Photo of arun maheshwari

arun maheshwari

17y
4.4k
1
how can i set the property for user/Custom controls

Answers (2)

0
Photo of arun maheshwari
NA 11 0 17y
thanx a lot for this information.
n ur way of explanation is good.
0
Photo of Nainil
NA 660 0 17y

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.