0

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 ScriptManager in userControl2 to register the button in the Page_Load event. So, How can I do this?

user204588
  • 1,613
  • 4
  • 31
  • 50

1 Answers1

1

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;
    }
ntziolis
  • 10,091
  • 1
  • 34
  • 50
  • That worked. Can you tell me why a regular find control would not work here? – user204588 Apr 22 '10 at 13:31
  • The ScriptManager might not be a direct child of the Page. Its might be inside other controls. This is usually the case when using master pages since master pages and content placeholders are considered controls too. – ntziolis Apr 22 '10 at 13:44
  • 1
    Wouldn't ScriptManager.GetCurrent be better? http://stackoverflow.com/questions/8601525/how-to-get-the-scriptmanager-placed-on-master-page-into-child-pages-code-behind – Adam Baxter Jun 19 '13 at 07:18