In aspx page i am write Page Method for call server side function and also set EnablePageMethods="True" in ScriptManager.
Default.aspx
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script type="text/javascript" language="javascript">
- function Displaymsg() {
- var name = document.getElementById('<%= txtName.ClientID %>').value;
- PageMethods.GetWelcomemsg(name, OnSuccess, onerror);
- }
- function OnSuccess(result) {
- if (result) {
- alert(result);
- return false;
- }
- }
- function onerror(error) {
- alert(error);
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
- </asp:ScriptManager>
- Enter Your Name:
- <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return Displaymsg();" />
- </div>
- </form>
- </body>
- </html>
Default.aspx.cs
- using System.Web.Services;
- [WebMethod]
- public static string GetWelcomemsg(string name)
- {
- return "Welcome " + name;
- }