Ajax练习:使用jQuery验证用户名是否存在
1.页面部分
用户名:<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<asp:Label ID="lblShow" runat="server" ForeColor="Red"></asp:Label>
2.编写javascript脚本部分
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script> <script type="text/javascript"> $(function () { $("#txtId").blur(function () { var a = $("#txtId").val(); $.ajax({ type: "post", url: "Handler1.ashx", data: { m: a }, success: function (result) { var res = result.toString(); $("#lblShow").html(res); } }); }); }); </script>
3.ashx中代码
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string Name = context.Request.Params["m"].ToString(); if (AdminManager.nameIsExist(Name) == ture) { context.Response.Write("此用户名已被注册!"); } else { context.Response.Write("此用户名可以使用!"); } }