在异步回发期间以编程方式添加 ServiceReference

发布于 2024-08-15 18:34:54 字数 1325 浏览 7 评论 0原文

是否可以在异步回发期间向 Page 上的 ScriptManager 添加新的 ServiceReference 实例,以便随后我可以通过客户端脚本使用引用的 Web 服务吗?

我试图在位于 Repeater 内的 UserControl 中执行此操作,这就是为什么在 Page_Load 期间以编程方式添加 ScriptReference 在这里不起作用。

编辑2:这是我从UserControl调用的代码,它没有达到我的预期(在异步回发期间将ServiceReference添加到ScriptManager):

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"));
}

我的目标是我的 UserControl 对周围的应用程序尽可能不显眼;否则我必须在包含的 Page 上的 ScriptManagerProxy 中静态定义 ServiceReference,这不是我想要的想。

编辑:

当我写这篇文章时我一定​​很累......因为我想写ServiceReference而不是ScriptReference。相应更新了上面的文字。

现在我有:

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

但我想在CodeBehind中注册web服务。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-08-22 18:34:54

尝试这样的事情......

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

Try something like this...

if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
    ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "script1", "~/js/myjs.js");
}
辞慾 2024-08-22 18:34:54

编辑:

检查您的问题后,以下是它不起作用的原因:
当您将ServiceReference添加到ScriptManager>时页面中的手动服务部分。它添加了 1 个客户端脚本包含指令。

例如:

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

或者

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

它为您提供了对 Frontend.Web.YourScriptMethods 的访问权限,

现在,当您在异步回发中添加 ServiceReference 时 - 它不会添加此客户端脚本因此您收到客户端脚本错误 - 方法未定义。

但我为此找到了解决方法(不知道这是正确的方法)

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

您可以根据您的项目/Web 服务替换“TestService.asmx”路径。

这样你就可以实现你想要的。

希望这会有所帮助,

Krunal Mevada


旧答案:

使用以下内容:

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

Edit:

After checking your issue, Here is the reason why it is not working:
When you add ServiceReference to ScriptManager > Services section manually in page. It adds 1 client script include directive.

e.g:

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

or

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

which provide you to accessibility to your Frontend.Web.YourScriptMethods,

Now when you add ServiceReference in async postback - It is not adding this client script inclue. So you get client script error - method is undefined.

But i figured out a workaround for this; (do not know it is right way to do it)

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

You can replace the "TestService.asmx" path according to your project/web service.

This way you can achieve what you want.

Hope this helps,

Krunal Mevada


Old Ans:

Use following:

    if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
    {
        ScriptManager.GetCurrent(this).Services.Add(new ServiceReference("~/Service.asmx"));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文