我的search.aspx页面中有一个.ascx用户控件。如何在后面的search.aspx.cs代码中从.ascx用户控件中获取控件?
keywordSearch.Value = "value";
// the code behind can't see the keywordSearch control发布于 2011-08-16 04:22:57
通常,内部控件不会从模板化的用户控件中公开,因为它们被声明为protected。但是,您可以在公共属性中公开控件,如下所示:
public TextBox CustomerName {
get { return txt_CustomerName; }
}编辑:如果你需要设置控件的值,那么你最好使用一个公开该值的属性,而不是控件:
public string CustomerName {
get { return txt_CustomerName.Text; }
set { txt_CustomerName.Text = value; }
}发布于 2011-08-16 04:22:59
您可以在用户控件的代码中提供一个公共(或内部)属性,以允许“获取”用户控件中的控件。然后,您可以从页面的代码后台访问该属性。
发布于 2011-08-16 04:24:40
尝试使用FindControl方法访问容器页上的控件:
((TextBox)Page.FindControl("keywordSearch")).Value = "value";https://stackoverflow.com/questions/7070343
复制相似问题