c# - Page_Load is been fired when WebMethod is called through javascript PageMethods - Stack Overflow

I have an aspx page. I've added a ScriptManager to it, and set EnablePageMethods=true, and created

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
Add a ment  | 

4 Answers 4

Reset to default 3

So 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745313931a4622128.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信