Page Methods in ASP.NET

We cannot call the server side code directly into client side. The server side code will execute at server and client code at client. However by using some .Net techniques we can call server side code from JavaScript by using AJAX methods or ASP.NET AJAX Extensions.

There are two ways to achieve this work one is web services (.asmx) and next is Page Methods. We need to call one of these methods from client side. A PageMethod is basically a public static method that is exposed in the code-behind of an aspx page and is callable from the client script. PageMethods are annotated with the [WebMethod] attribute. The page methods are rendered as inline JavaScript.

Page Methods are simple RPC methods that take JSON POST data inputs and return a JSON encoded string of the result value from the method call and they can be readily called from jQuery AJAX calls from the client
We need to declare page method at page level and can access using JavaScript function like below.

function pageLoad(sender, args)
tableView = $find(“”).get_masterTableView();

commandName = “Load”;

function OnSucess(result)

function OnFail() {
function UpdateGrid(result)
// masterResult = result;

Add SQL configuration to Web.config as

Now I am creating Employee class and Employee mapper class and make call to database in order get the employee details and bind that value to this class properties and load to grid view.

.cs code will look like this

public class EmployeeDetails
public string ID{ get; set; }
public string EmpName { get; set; }
public string PhNumber { get; set; }
public string Salary { get; set; }
public string Designation { get; set; }


public class ReportMapper

public static List GetDataAsListCollection(DataTable dt)

List employee = new List();
foreach (DataRow drRow in dt.Rows)

employee.Add(new EmployeeDetails ()
ID = drRow[“ID”].ToString(),
EmpName = drRow[“EmpName”].ToString(),
PhNumber =drRow[“PhNumber”].ToString(),
Salary =drRow[“Salary”].ToString(),
Designation =drRow[“Designation”].ToString()


return employee;
public static string GetEmployeeDetails(string empid)
if (empid == null || empid.Length == 0)
return String.Empty;
SqlConnection conn = null;
string connection =ConfigurationManager.ConnectionStrings[“mysqlprovider”].ConnectionString;
conn = new SqlConnection(connection);
string sql = “Select *from Employee where EmpId = @EmpID”;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue(“EmpID”, empid);
string getemp = Convert.ToString(cmd.ExecuteScalar());
return getemp;
catch (SqlException ex)
return “error”;

public static List getEmployeeData()
KPIReporting kpi = new KPIReporting();
DataSet ds = KPIReporting.GetEmployeeDetails(EmpID);
if (ds.Tables.Count > 0)
return ReportMapper.GetDataAsListCollection(ds.Tables[0]);

return null;


I have done some POC on these page methods and liked this technique much. It’s very simple technique to call server side code at client. Hope it will help.

Thank you!

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

%d bloggers like this: