Call ASPX methods from the front end using PageMethods and not AJAX

In this article I will explain how to use and make call ASP.Net AJAX PageMethods using ASP.Net AJAX and ScriptManager.
ASP.Net AJAX ScriptManager allows you to call Server Side methods from client side without any PostBack using PageMethods. Actually it is an AJAX call to the server but it allows us to call the method or function defined server side.
Enabling PageMethods
The first thing you need to do is add a ASP.Net AJAX ScriptManager to the page and set its EnablePageMethods property to true as shown below
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePageMethods=”true”>


HTML Markup
<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePageMethods=”true”>
Your Name :
<asp:TextBox ID=”txtUserName” runat=”server” ></asp:TextBox>
<input id=”btnGetTime” type=”button” value=”Show Current Time” onclick=”ShowCurrentTime()”/>


As you noticed above I have added a textbox when user can enter his name and a HTML button that calls a JavaScript method to get the Current Time.
Client Side Methods
<script type=”text/javascript”>
function ShowCurrentTime() {
PageMethods.GetCurrentTime(document.getElementById(<%=txtUserName.ClientID%>).value, OnSuccess);
function OnSuccess(response, userContext, methodName) {


Above the ShowCurrentTime method makes an AJAX call to the server using ASP.Net AJAX ScriptManager PageMethods and executes the GetCurrentTime method which accepts the username and returns a string value.
Server Side Methods
public static string GetCurrentTime(string name)
return“Hello “ + name + Environment.NewLine + “The Current Time is: “
        + DateTime.Now.ToString();


<System.Web.Services.WebMethod()> _
Public Shared Function GetCurrentTime(ByVal name AsString) AsString
Return“Hello “& name & Environment.NewLine &“The Current Time is: “& _


The above method simply returns a greeting message to the user along with the current server time. An important thing to note is that the method is declared as static (C#) and Shared(VB.Net) and also it is declared as Web Method unless you do this you won’t be able to call the methods using ASP.Net ScriptManager PageMethods.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.