ScriptManager in a User Control inside a UserControl
I have an asp.net user control, userControl1.ascx, and another user control, userControl2.ascx. userControl2 is inside userControl1. userControl1 is inside an UpdatePanel control. userControl2 has a button in it then I want to have do a normal post back when pushed. I want to use ScriptManager.RegisterPostBackControl(button). I have a ScriptManager on a master page. I don't know how to access the Scr开发者_如何学CiptManager in userControl2 to register the button in the Page_Load event. So, How can I do this?
You can find the script manager by using a recursive FindControl method. This is not best practice but it will get the job done. The is not really a pretty way to do this.
var scriptManager = FindControl(Page, "IdOfScriptManager");
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
精彩评论