Returning a value from a PageMethod using MS AJAX

NOTE: This is designed for v1.0.61025.0 of AJAXExtensionsToolbox.dll

If you've ever returned data from a web service using MS AJAX this technique will look familar.

Call the method from javascript by append PageMethods. to the front of the method name. Add a delegate to the end of the method's parameters to get the result.

<script language="javascript" type="text/javascript">
    function MyMethod_Result(ResultString)
    {  
        alert(ResultString);
    }
    
    function CallMyMethod(){
        PageMethods.MyMethod("World", MyMethod_Result);
    }
</script>

I have made the source to this available to download, otherwise take a look at the full explanation below:

Full explanation - Step 1 - Enable Page Methods on your ScriptManager

Set the EnablePageMethods attribute to true

<asp:ScriptManager ID="ScriptManager1" 
    EnablePageMethods="true" 
    EnablePartialRendering="true" runat="server" />

Step 2 - Mark your method as static and give it the WebMethod attribute

The method has to be declared static. It must also be marked with the WebMethod attribute. You'll probably find that you need to include System.Web.Services

using System.Web.Services;
[WebMethod]
public static string MyMethod(string name)
{
    return "Hello " + name;
}

Step 3 - Call it from Javascript

Call the method from javascript by append PageMethods. to the front of the method name. Add a delegate to the end of the method's parameters to get the result.

<script language="javascript" type="text/javascript">
    function MyMethod_Result(ResultString)
    {  
        alert(ResultString);
    }
    
    function CallMyMethod(){
        PageMethods.MyMethod("World", MyMethod_Result);
    }
</script>
Author Paul Hayman

Paul is the COO of kwiboo ltd and has more than 20 years IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.

Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

diamond_king said:

very good solution thank man

23/Jun/2008 10:34 AM

rajeep said:

Just what I was looking for !

thankyou

23/Jul/2008 08:47 AM

narendar said:

thanks

27/Sep/2008 15:20 PM

Nilesh Raut said:

Very Nice dear thanks alot

18/Dec/2008 13:20 PM

Tom said:

Hi,

i'm trying your code and i get error "PageMethods is undefined"

here's my example code:

<pre>

<script type="text/javascript" language="javascript">

function CountDown(myDiv)

{

   //my script here
   PageMethods.SignOut(OnCallComplete,OnCallError);

}

function OnCallComplete(userContext,methodName)

{

   setTimeout("window.navigate('Default.aspx')",2000);

}

function OnCallError(error,userContext,methodName)

{

   if(error !== null) 
   {
      alert(error.get_message());
   }

}

</script>

<pre/>

code behind:

<pre>

    [WebMethod]
    public static void SignOut()
    {
        FormsAuthentication.SignOut();
    }

<pre/>

i already set my script manager just like yours..any idea how i can get my error fixed ?

07/Jul/2009 05:35 AM

Dario said:

I the pagemethod returns a structure how can I read it client side?

Thank you

14/Nov/2010 14:17 PM

David said:

Thanks man, That's EXACTLY what I was looking for, I owe you a beer ...

13/Jun/2012 11:31 AM

siva said:

Hi I am having some problem with the pagemethods. on button OnClientClick event i want call the following function.If its its true i want to execute some server side functions further..How to achieve that..??

 function SendFriendReq1(FriendReq) {

            PageMethods.CheckFriend(FriendReq, function (Res) {

                // Here I want to return True/false to OnClientClick Event..

            });            

        }
03/Nov/2012 06:26 AM

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.