ASP.NET Ajax PageMethods

October 4, 2008

ASP.NET Ajax PageMethods

Introduction

During the making of a  ASP.NET Ajax ScriptMethod
ASP.NET Ajax lecture
which I’m scheduled to
do on this Monday,
I made some code
examples that include
a PageMethods example.
In this post I’ll explain what
are PageMethods and how to
use them in your ASP.NET application.

What are PageMethods?

ASP.NET Ajax extensions came with full support for script services. But sometimes
you don’t want to build a web service for a small piece of code or for basic functionality.
This is why the PageMethods feature was created.
The PageMethods feature enables the using of code-behind page methods on the client
side. The PageMethods can only be added to the page itself so don’t try to add them to
user controls or custom controls – it won’t work.

How to Use PageMethods?

Basically, all you need to do in order to use a PageMethod is to decorate your page
method with the ScriptMethod and WebMethod attributes or only with WebMethod
attribute. Another restriction is that the method have to be static (or else it won’t work).
After the decoration of the method and also the existence of ScriptManager in your page
the method can be accessed from the client side using the PageMethods object.

PageMethods Example

Lets look at a simple example of how to use PageMethods.
In the page I have put the following method which returns a “Hello” string:

   [ScriptMethod, WebMethod]

   public static string GetLabelText()

   {

      return "Hello";

   }

On the client side I have the following scripts which run that method:

    <script type="text/javascript">

      function InsertLabelData() {

          PageMethods.GetLabelText(onSuccess, onFailure);

      }

 

      function onSuccess(result) {

          var lbl = document.getElementById(‘lbl’);

          lbl.innerHTML = result;

      }

 

      function onFailure(error) {

          alert(error);

      }

      InsertLabelData();

    </script>

Also, in the page I hold a single Html label with a lbl id.
When the page is loading the “Hello” string will be attached to the label.
Pay attention to the PageMethods.GetLabelText call which calls the server side
method from the client side.

Summary

Lets sum up the post, I explained what are PageMethods.
I also showed a simple example of how to use a PageMethod.
The PageMethods feature is a very nice feature that you can use in the arsenal
of Ajax tools.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

5 comments

  1. ttklf4December 10, 2008 ב 11:14

    Hello,

    I try to read a JSON string which calls a page method the with a HTTP Module and I always get the following error message:

    Invalid web service call, missing value for parameter: name

    It seems that the JSON string is no more forwarded to the page if I read the body of an incoming HTTP request.

    Are there any workarounds?

    Thank you.

    Reply
  2. HemantJanuary 29, 2009 ב 9:43

    I have implemented the code but there is some thing went wrong :

    I am sending you the code which is quite simple and small:

    Default.aspx
    ===================================================




     





    Default.aspx.cs
    ===================================================
    [System.Web.Services.WebMethod()]
    public static string MyFirstPageMethod()
    {
    return “Welcome To The First called Function”;
    }

    ===================================================

    It is generating an error which is :->

    PageMethod undefined

    Thanks

    Reply
  3. Gil FinkJanuary 29, 2009 ב 13:54

    @Hemant,
    Put the call to CallFrmBack at the end of the page and not in the head. When the script is in the head, calling the CallFrmBack method will occur before the ScriptManager is loaded and therefore the PageMethods wouldn’t be defined in the page.

    Reply
  4. AshokFebruary 8, 2009 ב 8:08

    How to get rid of the “‘PageMethods’ is undefined” error message? My code is similar to what Hemant is trying and whichever position in the page I place the script tag(at the top or at the end) I get the same message.

    Reply
  5. shitaFebruary 13, 2009 ב 16:14

    Thanks for your articles. It’s useful to me.

    Reply