Adding ServiceReference programmatically during asynchronous postback

Is it possible to add a new instance in during an asynchronous postback so that I can subsequently use the referenced web service through the client side of the script? ServiceReferenceScriptManagerPage

I'm trying to do this inside UserControl, which is inside Repeater, so using the ScriptReference software programmatically while Page_Loadnot working here.

EDIT 2: This is the code I call from mine UserControlthat does not do what I expect (adding ServiceReference to the ScriptManager during 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 to make my my UserControl as unobtrusive to the surrounding application as possible; otherwise, I would have to statically define in in containing what I don't want. ServiceReferenceScriptManagerProxyPage

EDIT:

I must have been tired when I wrote this message ... because I wanted to write no . Updated text above. ServiceReferenceScriptReference

Now I have:

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

but I want to register webservice in CodeBehind.

+3
source share
2 answers

Edit:

, : ServiceReference ScriptManager > Services . 1 script include.

:

<script src="TestService.asmx/jsdebug" type="text/javascript"></script>

<script src="TestService.asmx/js" type="text/javascript"></script>

Frontend.Web.YourScriptMethods,

, ServiceReference async postback - script . , script - undefined.

; ( , )

    if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
    {
        ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "testservice", "TestService.asmx/js");
    }

"TestService.asmx" /-.

, .

, ,


Ans:

:

    if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
    {
        ScriptManager.GetCurrent(this).Services.Add(new ServiceReference("~/Service.asmx"));
    }
+1

- ...

if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
    ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "script1", "~/js/myjs.js");
}
+2

Source: https://habr.com/ru/post/1726261/


All Articles