You are not Logged in
Would you like to Login or Register

Today is: Friday, 21 November, 2008
Check this months hot topics

Calling a static "page method" from Javascript using MS AJAX

Atlas gave us the ability to easily call Web Services from JavaScript. MS AJAX has gone one step further! We can now call methods in the codebehine of the current page from Javascript. Here's how:

This is designed for v1.0.61025.0 of AJAXExtensionsToolbox.dll

Enable Page Methods on your ScriptManager

Set the EnablePageMethods attribute to true

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

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;
}

Call it from Javascript

To call the method from javascript you need to append PageMethods. to the front of the method name.

<script>
    function test(){
        alert(PageMethods.MyMethod("Paul Hayman"));
    }
</script>
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : Wednesday, 11 July, 2007

Paul is the COO of kwiboo ltd consultant and has more than a decade of 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

Anastasiosyal said:

This is cool! What about security? If you needed to be logged in to execute "MyMethod", does the xmlhttpRequest issued contain a session cookie with it?

July 11, 2007 - 2:07 PM

phayman said:

This is something I considered myself.. what I decided to do in my implementation was check that the user was in one of the allowed rolegroups for the operation.

To clarify, I used this for passing back information from a content editor. So, a check to see if the user was an Administrator or Editor was sufficient.

Paul

July 11, 2007 - 2:14 PM

Wouter said:

Hi, I get an exception "PageMethods is not defined". Any ideas? is PageMethods an internal js object or am I missing something?

July 26, 2007 - 9:59 AM

phayman said:

After discussing it with Wouter we discovered that his problem was caused by the WebMethod being located inside a UserControl, not the Page.

It would appear that you can't call a WebMethod located in a UserControl.

If anyone knows how to achieve this, post a comment.

Paul

July 26, 2007 - 10:51 AM

Andres V said:

I'm able to hit the server method, but it is returning me "undefined" instead of the string "Hello whatever".

What am I doing wrong?

August 17, 2007 - 11:04 PM

Nelero said:

I'm facing with the same problem as Andres V. When I call my webservice method, returning a string, i get "undefined"...

August 27, 2007 - 6:37 PM

Gerard van de Ven said:

Yes, the actual way of doing this is to use a "onComplete" because of the asynchronous call, as follows:

  <script type="text/javascript">

    function test(){

        PageMethods.MyMethod("Gerard van de Ven", OnMyMethodComplete);

    }



    function OnMyMethodComplete(result, userContext, methodName)

    {

      alert(result);

    }

  </script>

October 02, 2007 - 9:20 PM

nik said:

excellent, thanks

March 31, 2008 - 3:14 PM

phayman said:

Not marking the PageMethod as STATIC is quite a common mistake, be sure to mark your metdod as STATIC :)

March 31, 2008 - 3:54 PM

lkeel said:

I am having a problem calling my webmethod from javascript. Everything was working fine until I implemented security. Now when I do PageMethod.TestCall("something"); I get a 401 - Authentication Error. Do you have any ideas on how to resolve this problem?

May 29, 2008 - 5:41 PM

Lkeel said:

I am trying to make a call back to my server from the javascript the same as you have here. My problem is that now that I have secured my website with forms authentication, the call back to the server is throwing a 401-Authentication error. Have you seen this before? Do you have a work around for this problem?

June 02, 2008 - 3:06 PM

rajeep said:

just what i was looking for thanks

July 23, 2008 - 8:47 AM

Sqeeb said:

Having problems here....

I'm using the same code verbadum (enabling scriptManager page methods, your javaScript and codebehind method) on a master page. For some reason, it's not liking PageMethods and won't call the server side function. Any Suggestions?

August 21, 2008 - 6:53 PM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views