Grabbing a control from an ascx page in ASP.Net
I have an .ascx开发者_StackOverflow user control that is in my search.aspx page. How do I grab a control from the .ascx user control in the search.aspx.cs code behind ?
keywordSearch.Value = "value";
// the code behind can't see the keywordSearch control
Normally inner controls are not exposed from templated user controls, because they're declared as protected
. You can however expose the control in a public property, like this:
public TextBox CustomerName {
get { return txt_CustomerName; }
}
Edit: If you need to set the value of the control then you're better off with a property that exposes the value, not the control:
public string CustomerName {
get { return txt_CustomerName.Text; }
set { txt_CustomerName.Text = value; }
}
You might be able to provide a public (or internal) property in your user control's code behind that allows "getting" the control in the user control. You could then access that property from your page's code behind.
Try FindControl method to access a control at container page :
((TextBox)Page.FindControl("keywordSearch")).Value = "value";
精彩评论