Enrich Your Web Apps

Call a Server-Side Page Method with AJAX

Exploring ASP.NET & Web Development


Enrich Your Web Apps

Call a Server-Side Page Method with AJAX

By Don Kiely


One of the interesting things you can do with client-side JavaScript using the ASP.NET AJAX Client Library is to call a page method from client code. This means that if you have an existing page with a server-side method, you can call the method from client-side JavaScript. And, it is done without a full or partial page postback.


This may seem to be a bit counter-intuitive. Doesn t server-side code in a Web page in ASP.NET by definition require a postback? Not at all. The way ASP.NET allows this is by turning the method into a proxy Web service that is callable from client-side script, very much like calling a regular ASP.NET Web service.


There are just a few things you need to do differently when calling a page method instead of a Web service, so here I ll focus on the differences. First, of course, the code behind the page needs to have a method to call, and here I ll use GetInventory. The following code implements that method, and includes the WebMethod attribute to make it callable from script. It must be a static/shared method, and you don t have to use the ScriptService attribute on the class. The code in the method itself is pretty typical ADO.NET code, taking a product ID for the Northwind database and returning the number of that product in inventory. This code is C#, but you can do the same sort of thing in Visual Basic:



public static short GetInventory(int ProductID)


short retVal = 0;

string strCnn =


string strSQL = "SELECT UnitsInStock FROM Products " +

"WHERE ProductID = @ProductID";

SqlConnection cnn = new SqlConnection(strCnn);


SqlCommand cmd = new SqlCommand(strSQL, cnn);

cmd.Parameters.Add(new SqlParameter("@ProductID",




retVal = (short)cmd.ExecuteScalar();


catch (Exception e)


if (e.Message ==

"Object reference not set to an instance of an object.")

throw new Exception("Invalid product ID. Please try another.");








return retVal;



The ScriptManager control as used in the page that calls the method is defined a little differently in order to invoke a page method. You don t need a Services element to identify the Web service that the script will call. You do need, however, to set the EnablePageMethods attribute to true. Below is the ScriptManager control in the page:


<asp:ScriptManager ID="ScriptManager1"

 runat="server" EnablePageMethods="true">



The only other change is that the client-side getInventory function must call the page method instead of a Web service. It does this using the PageMethods object, which was enabled by setting the ScriptManager s EnablePageMethods attribute to true. (The page won t recognize the PageMethods object without that attribute setting.) Otherwise, the call to GetInventory is pretty much the same as calling any Web service:


function getInventory()



getInventorySuccess, getInventoryFail);



If you ve shied away from enriching your Web applications with client-side JavaScript code in the past, you might find that the AJAX Client Library makes it easy enough to consider using in the future. You ll need to learn JavaScript a bit beyond the basics, because the library builds on JavaScript that might be confusing if you re only familiar with server-side .NET languages. But the possibilities for enriching the usability and value of your applications will almost certainly be worth it in the long run. Besides, it is likely that this is the direction we re headed in the future, enhancing the client-side features of the Web. The server side may just become the dark side of the future!


Don Kiely, MVP, MCSD, is a senior technology consultant, building custom applications as well as providing business and technology consulting services. His development work involves tools such as SQL Server, Visual Basic, C#, ASP.NET, and Microsoft Office. He writes regularly for several trade journals, and trains developers in database and .NET technologies. You can reach Don at mailto:[email protected] and read his blog at http://www.sqljunkies.com/weblog/donkiely/.





Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.