ASP.NET中前台调用后台的方法

学习文章:http://www.cnblogs.com/kingteach/archive/2010/11/12/1875633.html

练习代码:

前台:

 1 <html xmlns="http://www.w3.org/1999/xhtml">

 2 <head runat="server">

 3     <title></title>

 4     <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

 5     <script language="javascript" type="text/javascript">

 6         $(document).ready(function () {

 7         });

 8 

 9         function CheckUnitNameExist() {

10             var name = $("#TextBox1").get(0).value;

11             PageMethods.CheckName(name, jsFunc);

12         }

13         function jsFunc(result) {

14             alert(result);

15         }

16     </script>

17 </head>

18 <body >

19 

20     <form id="form1" runat="server">

21      <asp:ScriptManager ID="ScriptManager1" runat="server"  EnablePageMethods="true">  </asp:ScriptManager>

22     <div>

23         请输入姓名:

24         <asp:TextBox ID="TextBox1"  runat="server" Width="200" onchange="CheckUnitNameExist()"></asp:TextBox>

25     </div>

26     </form>

27 </body>

28 </html>
View Code

后台:

 1 using System.Collections.Generic;

 2 using System.Linq;

 3 using System.Web;

 4 using System.Web.UI;

 5 using System.Web.UI.WebControls;

 6 using System.Web.Services;

 7 using System.Web.Script.Services;

 8 

 9 namespace htmltest

10 {

11     public partial class WebForm7 : System.Web.UI.Page

12     {

13         protected void Page_Load(object sender, EventArgs e)

14         {

15            

16         }

17 

18         [WebMethod]

19         public static string CheckName(string UnitName)

20         {

21             if (UnitName == "苏州")

22             {

23                 return "苏州的名字已存在!";

24             }

25             else

26             {

27                 return "可随意填写!";

28             }

29         }

30 

31     }

32 }
View Code


注意事项见学习文章.

你可能感兴趣的:(asp.net)