tags:

views:

335

answers:

2

Hi All, I have a user control which has a textbox on it, now this usercontrol is on another user control which I am using on aspx page how can I get value of the textbox on the first user control.

+2  A: 

Write a property in your usercontrol to expose its contents, e.g.,

public string TextBoxValue
{
    get { return txtControl1.Text; }
}

This way you can get the value of the textbox without exposing the whole textbox control as a public object.

Jon Limjap
A: 

Jon Limjap's answer provides the best solution for this kind of problem - Expose control values using Public properties.

However, if you do not want to do it this way (or you have to do this for a lot of controls and want to avoid creating Public properties for each control), you could use Reflection to "find the control" in the ChildControls of the required UserControl:

TextBox txt = UserControl1.FindControl("myTextBox") as TextBox;

if (txt != null)
{
  string val = txt.Text;
}
Cerebrus