简体   繁体   English

C#用户控件:访问控件属性

[英]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 我已经创建了一个控件并在该控件中添加了一个TextBox,我将该控件附加到.aspx页面

<%@ 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 ? 在.aspx.cs文件中我想访问用户控件里面的TextBox的值...如何实现?

Add a Public Property in AccountSearchControl.ascx 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). 默认情况下,您放置在页面上的所有控件都具有protected可见性(请参阅AccountSearchControl.ascx.designed.cs以查看)。 So you need to expose a method for your page to access the Textbox. 因此,您需要为您的页面公开一种方法来访问文本框。

you want something like this on your usercontrol 你想在你的usercontrol上想要这样的东西

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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