DCSIMG
ASP.NET Ajax PageMethods - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

ASP.NET Ajax PageMethods

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.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 4, 2008 9:28 AM

ttklf4 said:

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.

# December 10, 2008 11:14 AM

Hemant said:

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

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

<html>

<body>

<script language="javascript">

   var intInterval = 0

   function CallFrmBack()

   {

  PageMethods.MyFirstPageMethod(onSuccess,onFailed);

   }

   function onSuccess(result)

   {

    var lbl = document.getElementById('TextBox4');

    lbl.innerHTML = result;

   }

   function onFailure(error)

   {

        alert(error);

   }

   CallFrmBack();

</script>

 <form id="form1" runat="server">

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

   <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />

   <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>&nbsp;<br />

   <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="66px" Width="288px"></asp:TextBox> <br />

<button onclick="intInterval=window.setInterval('CallFrmBack()', 4000);">Start interval</button>

<button onclick="intInterval=window.clearInterval(intInterval);">Stop interval</button>

     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

   </form>

</body>

</html>

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

# January 29, 2009 9:43 AM

Gil Fink said:

@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.  

# January 29, 2009 1:54 PM

Ashok said:

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.

# February 8, 2009 8:08 AM

shita said:

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

# February 13, 2009 4:14 PM

How to pass value to the javascript on partial post back using updatePanel - Programmers Goodies said:

Pingback from  How to pass value to the javascript on partial post back using updatePanel - Programmers Goodies

# November 16, 2011 7:50 PM