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

Asp.Net Call Code Behind (Server Side) Function from JavaScript in C#, VB.Net

Aug 3, 2016
Introduction

Here I will explain how to call code behind method / function from JavaScript in
asp.net using c#, vb.net with example or asp.net call code behind / server side method from JavaScript using c#, vb.net or how to call code behind method from JavaScript in asp.net using c#, vb.net with example. By using asp.net ajax scriptmanager property “EnablePageMethods = True” we can call server side method  in c#, vb.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 call code behind method or function from JavaScript we need to write the following code in aspx page.


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>call server side function 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>

Now open code behind file and write following code

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

Call Code Behind (Server Side) Function from JavaScript in Asp.Net using C#, VB.Net

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

1 comments :

hardik maheshwari said...

not work

Give your Valuable Comments

Other Related Posts

© 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.