简体   繁体   中英

C# User Controls: access controls properties

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 ?

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.

you want something like this on your usercontrol

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

这是一种访问用户控件内的文本框控件的方法:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM