Add AJAX Service to Page’s ScriptManager in run-time

The only way to expose AJAX functionality, client-side Web Service calls (.asmx file) when you are working with a UserControl and don’t have access to the Page (because of the nature of the environment), is to add a Script Manager and the WebService programatically in code-behind (run-time).


protected override void OnInit(EventArgs e)
    Page.Init += delegate(object sender, EventArgs e_Init)
        var sm = System.Web.UI.ScriptManager.GetCurrent(Page);

        if (sm == null)
            sm = new System.Web.UI.ScriptManager();
            Page.Form.Controls.AddAt(0, sm);

        var sr = new System.Web.UI.ServiceReference("/App_Service/MyWebService.asmx");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s