2

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 ?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

you want something like this on your usercontrol

public string textBoxValue
{ 
    get { return this.myTextBoxId.Text; }
    set { this.myTextBoxId.Text = value; }
}

Comments

2

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

TextBox yourTextBox = (TextBox)AccountSearchControlBox.FindControl("your_textbox_ID");

1 Comment

this one is not problem, but sometimes you need to access a control at parent.. that case is really annoying :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.