14

My Pagemethod implementation is not working in Chrome browser. I have ASP.NET 3.5 web application developed in VS 2008.

The code below not working in chrome or Safari:

function FetchDataOnTabChange(ucName)
{ 
    PageMethods.FetchData(ucName, OnSuccessFetchDataOnTabChange, OnErrorFetchDataOnTabChange);
}

function OnErrorFetchDataOnTabChange(error)
{   
   //Do something
}

function OnSuccessFetchDataOnTabChange(result)
{
   //Do something  
}
Rup
  • 33,765
  • 9
  • 83
  • 112
Chetan
  • 1,517
  • 3
  • 17
  • 25

1 Answers1

40

This should work in all browsers by following the steps below:

  • The page method must have the System.Web.Services.WebMethod attribute. [WebMethod]
  • The page method must be public. [WebMethod] public ...
  • The page method must be static. [WebMethod] public static ...
  • The page method must be defined on the page (either inline or in the code-behind). It cannot be defined in a control, master page, or base page.
  • The ASP.NET AJAX Script Manager must have EnablePageMethods set to true.

This is from a working application

aspx page:

/* the script manager could also be in a master page with no issues */
<asp:ScriptManager ID="smMain" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
    function GetDetails(Id) {
        PageMethods.GetDetails(doorId);
    }
</script>

code behind:

[System.Web.Services.WebMethod]
public static void GetDetails(string Id)
{

}
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
hardba11
  • 1,478
  • 15
  • 27
  • I don't understand this: "The page method cannot be defined in base page". Could you explain why? Cause I have to use handlers then. – donRumatta Mar 26 '13 at 10:15
  • unfortunately, I don't know the technical reason "why", but the answer is because MasterPage does not inherit from System.Web.UI.Page therefore you can not call PageMethods since technically its not a page but actually a System.Web.UI.MasterPage. – hardba11 Apr 03 '13 at 19:59
  • 1
    Have to say that I think this answer is out of date. See my question here: http://stackoverflow.com/questions/20554780/inconsistent-pagemethod-behavior-in-different-browsers. Followed all of your steps, with mixed results. – LittleBobbyTables - Au Revoir Dec 13 '13 at 19:00
  • 2
    @ColoradoRockie, I think you are incorrect in saying "...cannot be defined in a control, master page, or base page..". This is completely possible. Define your 'Page Methods' in any class which is derived from ASP.NET 'Page' class. The scripts for that methods will be injected when a derived page is called. – James Poulose Aug 05 '14 at 05:48
  • 1
    Really getting tired of these garbage edits by people just trying to earn rep. Or in the case of A-Sharabiani - clearly trying to earn his Copy Editors Badge. Really? "Good Luck :-)" was detracting from this 8 year old post. – hardba11 Mar 11 '19 at 21:00
  • Information „It cannot be defined in base page“ is incorrect. In fact, Page method can be defined in base page class. – tibx Jan 31 '23 at 06:32