views:

124

answers:

1

Hello,

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?

+1  A: 

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
That worked. Can you tell me why a regular find control would not work here?
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