aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Call Server Side Function from JavaScript in Ajax

Dec 4, 2012

Here I will explain how to call server side methods or functions from
JavaScript in

In previous posts I explained How to Call Page methods in jQuery,
JavaScript restrict to enter only numbers, JavaScript Redirect to another page after some time delay, JavaScript Set Default home page in browsers and many articles relating to JavaScript, Now I will explain how to call server side methods or functions from JavaScript in

By using Ajax scriptmanager we can call server side methods from JavaScript for that we need to set property EnablePageMethods="True" in Ajax scriptmanager. 

To implement this one you need to write the following code in your page

<html xmlns="">
<head runat="server">
<title>call server side method from javascript in</title>
<script type="text/javascript" language="javascript">
function callservermethod() {
var name = $get("txtName").value;
PageMethods.GetCurrentDate(name, OnSuccess, OnFailure);
function OnSuccess(result) {
if (result) {
function OnFailure(error) {

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
Enter Name of Person: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnClick" runat="server" Text="Click" OnClientClick ="callservermethod()" />
After that add following namespace in code behind

C# Code

using System;
using System.Web.Services;
Once namespaces added then write the following code in code behind

public static string GetCurrentDate(string name)
return "Hi " + name + Environment.NewLine + "Welcome to ";

Imports System.Web.Services
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
<WebMethod()> _
Public Shared Function GetCurrentDate(ByVal name As String) As String
Return ("Hi " & name) + Environment.NewLine & "Welcome to "
End Function
End Class

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Mohit Bhati said...

hi suresh..have a doubt..

why have u made "GetCurrentDate" a [web method]??

cant it be a normal method? reply

Anonymous said...

best example

Anonymous said...

yes i too have a same doubt that can't we call a normal method?
I dont want it as webmethod. Can it be done?

Sushil Kumar said...

How to use external javascript file in

Srijith M said...

hai suresh
why have u made "GetCurrentDate" a [web method]??
cant it be a normal method?

Why webmethod have to be static ? reply

Lalit Raghuvanshi said...
This comment has been removed by a blog administrator.

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.