views:

43

answers:

3

Hi I've created user control named test.ascs with one textbox. Now I added this user control in my default.aspx page. How can i access that textbox value from my default.aspx page?

is there any chance?

+1  A: 

From your default page try to find the TextBox using your user control.

TextBox myTextBox = userControl.FindControl("YourTextBox") as TextBox;
string text = myTextBox.text;
KhanS
This method may well work, but it smells big time. You've got textbox IDs hard coded. You are not checking that the result is != null before trying to access its properties. Using FindControl is so much slower and inefficient than doing it in a proper OO fashioned way and exposing the properties on the user control.
slugster
+2  A: 

If this is the purpose of the control, then create a public property on your user control that exposes this value, you can then access that from your page:

string textBoxValue = myUserControl.GetTheValue;
Paddy
Where `myUserControl` is ID of UserControl in markup: `<uc:MyUserControl runat="server" ID="myUserControl" />`
abatishchev
+3  A: 

I usually expose the textbox's text property directly in test.ascx code behind like this:

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

Then you can get and set that textbox from the code behind of default.aspx like:

usrControl.Text = "something";
var text = usrControl.Text;
BritishDeveloper