Adding a ServiceReference programmatically during async postback

Posted by Oliver on Stack Overflow See other posts from Stack Overflow or by Oliver
Published on 2009-12-21T16:23:04Z Indexed on 2010/05/17 12:30 UTC
Read the original article Hit count: 331

Is it possible to add a new ServiceReference instance to the ScriptManager on the Page during an asynchronous postback so that subsequently I can use the referenced web service through client side script?

I'm trying to do this inside a UserControl that sits inside a Repeater, that's why adding the ScriptReference programmatically during Page_Load does not work here.

EDIT 2: This is the code I call from my UserControl which does not do what I expect (adding the ServiceReference to the ScriptManager during the async postback):

private void RegisterWebservice(Type webserviceType)
{
    var scm = ScriptManager.GetCurrent(Page);
    if (scm == null)
        throw new InvalidOperationException("ScriptManager needed on the Page!");

    scm.Services.Add(new ServiceReference("~/" + webserviceType.Name + ".asmx"));
}

My goal is for my my UserControl to be as unobtrusive to the surrounding application as possible; otherwise I would have to statically define the ServiceReference in a ScriptManagerProxy on the containing Page, which is not what I want.

EDIT:

I must have been tired when I wrote this post... because I meant to write ServiceReference not ScriptReference. Updated the text above accordingly.

Now I have:

<asp:ScriptManagerProxy runat="server" ID="scmProxy">
    <Services>
        <asp:ServiceReference Path="~/UsefulnessWebService.asmx" />
    </Services>
</asp:ScriptManagerProxy>

but I want to register the webservice in the CodeBehind.

© Stack Overflow or respective owner

Related posts about scriptmanager

Related posts about asynchronous-postback