How to call code behind method from jquery

Now let us see “How to call code behind method from jquery in asp.net”. It is very simple , try  it out.

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


[WebMethod]
public static string DisplayData()
{
return DateTime.Now.ToString();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>