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.
Description:
In previous articles I explained jQuery show asp.net exception error message in ajax call, jQuery bind dropdownlist in asp.net using json data, Send html page as email body in asp.net using c#, vb.net, jQuery remove all whitespaces in string with regex, jQuery show large image preview on hover in asp.net and many articles relating to asp.net, c#, vb.net, JavaScript and jQuery. Now I will explain how to call code behind function from JavaScript in asp.net using c#, vb.net with example.
In previous articles I explained jQuery show asp.net exception error message in ajax call, jQuery bind dropdownlist in asp.net using json data, Send html page as email body in asp.net using c#, vb.net, jQuery remove all whitespaces in string with regex, jQuery show large image preview on hover in asp.net and many articles relating to asp.net, c#, vb.net, JavaScript and jQuery. Now I will explain how to call code behind function from JavaScript in asp.net using c#, vb.net with example.
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
|
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. |
|||
|
|||
2 comments :
not work
I want to same solution but can i call Public void function, my function not static. if i make static function then response.redirect not work.
Note: Only a member of this blog may post a comment.