Pagemethods in asp.net
Asked Answered
J

1

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  
}
Junkman answered 30/11, 2010 at 12:6 Comment(3)
Yes its works better in IE7 and above, Mozilla properly, but fails in Chrome and Safari. and I dont want to comment on the last question.Junkman
you'll need to post some code if you want an answerTreiber
Can you also provide the code you use in javascript? There is a developer tool to debug javascript in Chrome too. ctrl + shift + jEngrain
G
41

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)
{

}
Gustave answered 29/12, 2010 at 22:57 Comment(6)
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.Kummerbund
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.Gustave
Have to say that I think this answer is out of date. See my question here: #20555280. Followed all of your steps, with mixed results.Zamindar
@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.Lithography
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.Gustave
Information „It cannot be defined in base page“ is incorrect. In fact, Page method can be defined in base page class.Pyrethrin

© 2022 - 2024 — McMap. All rights reserved.