Aspdotnet-Suresh

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

Call Server Side Function from JavaScript in Asp.net Ajax

Dec 4, 2012
Introduction

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

Description:
  
In previous posts I explained How to Call Asp.net 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, Asp.net. Now I will explain how to call server side methods or functions from JavaScript in asp.net.

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

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>call server side method from javascript in asp.net</title>
<script type="text/javascript" language="javascript">
function callservermethod() {
var name = $get("txtName").value;
PageMethods.GetCurrentDate(name, OnSuccess, OnFailure);
}
function OnSuccess(result) {
if (result) {
alert(result);
}
}
function OnFailure(error) {

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
Enter Name of Person: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnClick" runat="server" Text="Click" OnClientClick ="callservermethod()" />
</div>
</form>
</body>
</html>
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


[WebMethod]
public static string GetCurrentDate(string name)
{
return "Hi " + name + Environment.NewLine + "Welcome to Aspdotnet-Suresh.com ";
}
VB.NET Code

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 Aspdotnet-Suresh.com "
End Function
End Class
Demo


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

Get the latest Asp.net, C#.net, 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

6 comments :

Mohit Bhati said...

hi suresh..have a doubt..

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

cant it be a normal method? reply
bhatimohit@gmail.com

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?

Unknown said...

How to use external javascript file in asp.net.

Unknown said...

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

Why webmethod have to be static ? reply
mahasrijith@gmail.com

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

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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