学习文章:http://www.cnblogs.com/kingteach/archive/2010/11/12/1875633.html
练习代码:
前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
}); function CheckUnitNameExist() {
var name = $("#TextBox1").get(0).value;
PageMethods.CheckName(name, jsFunc);
}
function jsFunc(result) {
alert(result);
}
</script>
</head>
<body > <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
<div>
请输入姓名:
<asp:TextBox ID="TextBox1" runat="server" Width="200" onchange="CheckUnitNameExist()"></asp:TextBox>
</div>
</form>
</body>
</html>
后台:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services; namespace htmltest
{
public partial class WebForm7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } [WebMethod]
public static string CheckName(string UnitName)
{
if (UnitName == "苏州")
{
return "苏州的名字已存在!";
}
else
{
return "可随意填写!";
}
} }
}
注意事项见学习文章.