Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a ServiceReference programmatically during async postback

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.

like image 602
Oliver Avatar asked Dec 10 '25 22:12

Oliver


1 Answers

Try something like this...

if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
    ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "script1", "~/js/myjs.js");
}
like image 190
Krunal Avatar answered Dec 12 '25 13:12

Krunal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!