通过javascript PageMethods调用WebMethod时会触发Page_Load [英] Page_Load is been fired when WebMethod is called through javascript PageMethods
问题描述
我有一个aspx页面.我已经添加了一个ScriptManager,并设置了 EnablePageMethods = true
,并在服务器端创建了一个标记为[WebMethod]的静态方法.
我一直使用WebMethods,并且以前从未见过此错误.
在javascript上,可以访问PageMethods.但是,当我调用我的方法时,将触发 Page_Load
方法,而不是WebMethod.
我搜索后发现其他人也有此问题.但是没有答案....有什么想法吗?
I have an aspx page. I've added a ScriptManager to it, and set EnablePageMethods=true
, and created a static method marked as [WebMethod] on the server-side.
I have always worked with WebMethods, and I've never seen this error before.
On javascript, PageMethods is accessible. But when I call my method, the Page_Load
method is fired, instead of the WebMethod.
I've searched and found other people had this issue as well. But no answers.... Any ideas?
HTML:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
JS:
PageMethods.Test()
C#:
[WebMethod]
public static void Test()
{
}
推荐答案
我发现我所遇到的问题是我使用的是友好的URL.由于 PageMethods
通过其地址引用服务器端页面,因此存在问题.在CodePlex上已对此进行了讨论: http://aspnetfriendlyurls.codeplex.com/workitem/3 .
显然有解决方法,但是我最终对通用处理程序(.ashx)进行了手动ajax调用.
I discovered that the problem on my case is that I'm using friendly URL's. Since PageMethods
references the server-side page by its address, there you have the issue. It's been discussed here, on CodePlex: http://aspnetfriendlyurls.codeplex.com/workitem/3.
Apparently there are workarounds for this, but I ended up making a manual ajax call to a generic handler (.ashx).
这篇关于通过javascript PageMethods调用WebMethod时会触发Page_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!