在异步回发期间以编程方式添加 ServiceReference
是否可以在异步回发期间向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情......
Try something like this...
编辑:
检查您的问题后,以下是它不起作用的原因:
当您将ServiceReference添加到ScriptManager>时页面中的手动服务部分。它添加了 1 个客户端脚本包含指令。
例如:
或者
它为您提供了对 Frontend.Web.YourScriptMethods 的访问权限,
现在,当您在异步回发中添加 ServiceReference 时 - 它不会添加此客户端脚本。 因此您收到客户端脚本错误 - 方法未定义。
但我为此找到了解决方法; (不知道这是正确的方法)
您可以根据您的项目/Web 服务替换“TestService.asmx”路径。
这样你就可以实现你想要的。
希望这会有所帮助,
Krunal Mevada
旧答案:
使用以下内容:
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:
or
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)
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: