views:

366

answers:

3

I have created a control and added a TextBox inside that control, I am attaching that control to a .aspx page via

<%@ Register Src="../UserControls/AccountSearchControl.ascx" TagName="SearchControl"
TagPrefix="csr" %>

and

<csr:SearchControl ID="AccountSearchControlBox" runat="server"  OnSearchButtonClick="RetreiveAccounts" />

On .aspx.cs file I want to access the value of the TextBox inside the user control ... how to achieve that ?

+2  A: 

Add a Public Property in AccountSearchControl.ascx

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

By default all of the Controls you place on the page have a protected visibility (Take a look at AccountSearchControl.ascx.designed.cs to see). So you need to expose a method for your page to access the Textbox.

Bob
+2  A: 

you want something like this on your usercontrol

public string textBoxValue
{ 
    get { return this.myTextBoxId.Text; }
    set { this.myTextBoxId.Text = value; }
}
Andrew Bullock
+1  A: 

Here is a way to access a textbox control inside an user control :

TextBox yourTextBox = (TextBox)AccountSearchControlBox.FindControl("your_textbox_ID");
Canavar
dirty webforms madness
Andrew Bullock
this one is not problem, but sometimes you need to access a control at parent.. that case is really annoying :)
Canavar