admin管理员组文章数量:1419583
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()
{
}
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()
{
}
Share
Improve this question
asked May 9, 2014 at 20:26
LcSalazarLcSalazar
16.9k3 gold badges40 silver badges71 bronze badges
4
-
What needs to happen for the JS call to
PageMethods.Test()
to be triggered? A button click? – Sven Grosen Commented May 9, 2014 at 21:03 -
Possibly.. or any other way. The
PageMethods.Test()
is been called correctly. – LcSalazar Commented May 9, 2014 at 21:05 -
Right, but if it is through a button click and you aren't preventing that click to result in a call back to the server (via
event.preventDefault()
), that would trigger a page load. – Sven Grosen Commented May 9, 2014 at 21:07 - That's true, but it's not the case. I tried calling it through different ways, and it is always the same. I've created another solution, and pasted the same code, and the WebMethod is called correctly. There must be something I'm missing out in the project I'm working with.... Perhaps something on the web.config... – LcSalazar Commented May 12, 2014 at 12:47
4 Answers
Reset to default 3So after founding the culprit of why controls in my update panel was calling post back twice I found it was because of a page method being called.
After finding LcSalazar solution I did disabled Friendly URLs and everything was working. But I find the Friendly URLs to be more clean so I found a solution .
On your master page add the following .
<script type="text/javascript">
$(function () {
try {
if (PageMethods.get_path().indexOf('.aspx') == -1)
PageMethods.set_path(PageMethods.get_path() + '.aspx');
} catch (e) {
}
})
</script>
Once the page methods path includes the .aspx the page_load doesn't fire again.
Alternatively look into using ASP.Net Web Api . Better practice as well when it es to reusable methods instead of declaring the methods in you Base Page or every page you are trying to use it .
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./workitem/3.
Apparently there are workarounds for this, but I ended up making a manual ajax call to a generic handler (.ashx).
Disable friendly urls. Comment this out in App_Start\RouteConfig.cs routes.EnableFriendlyUrls(settings);
When you submit a form that has runat server it processes the full lifecycle. Which fires the Page_Load of your .cs class. If you want to make it strictly want to fire the webmethod I suggest using an ajax call to your webmethod.
本文标签: cPageLoad is been fired when WebMethod is called through javascript PageMethodsStack Overflow
版权声明:本文标题:c# - Page_Load is been fired when WebMethod is called through javascript PageMethods - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745313931a2653063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论