C# 从ASP.Net中的ascx页抓取控件
我的search.aspx页面中有一个.ascx用户控件。如何从search.aspx.cs代码中的.ascx用户控件中获取控件C# 从ASP.Net中的ascx页抓取控件,c#,asp.net,webforms,C#,Asp.net,Webforms,我的search.aspx页面中有一个.ascx用户控件。如何从search.aspx.cs代码中的.ascx用户控件中获取控件 keywordSearch.Value = "value"; // the code behind can't see the keywordSearch control 通常,内部控件不会从模板化用户控件中公开,因为它们被声明为受保护的。但是,您可以在公共属性中公开控件,如下所示: public TextBox CustomerName { get {
keywordSearch.Value = "value";
// the code behind can't see the keywordSearch control
通常,内部控件不会从模板化用户控件中公开,因为它们被声明为受保护的。但是,您可以在公共属性中公开控件,如下所示:
public TextBox CustomerName {
get { return txt_CustomerName; }
}
编辑:如果需要设置控件的值,则最好使用公开值的属性,而不是控件:
public string CustomerName {
get { return txt_CustomerName.Text; }
set { txt_CustomerName.Text = value; }
}
您可以在用户控件的代码隐藏中提供公共(或内部)属性,以允许“获取”用户控件中的控件。然后,您可以从页面的代码隐藏中访问该属性。尝试FindControl方法访问容器页面上的控件:
((TextBox)Page.FindControl("keywordSearch")).Value = "value";
如何编写setter呢?如果还需要设置值,则应根据此处讨论的控件将属性公开为字符串或其他类型,然后在访问器中获取/设置值。您希望为控件的值公开一个getter,而不是为控件本身公开一个setter。