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

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

Aug 3, 2016

Here I will explain how to call code behind method / function from JavaScript in using c#, with example or call code behind / server side method from JavaScript using c#, or how to call code behind method from JavaScript in using c#, with example. By using ajax scriptmanager property “EnablePageMethods = True” we can call server side method  in c#,

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 call code behind method or function from JavaScript we need to write the following code in aspx page.

<html xmlns="">
<head id="Head1" runat="server">
<title>call server side function 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()" />

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

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


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,, 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


hardik maheshwari said...

not work

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.