Ajax实现无刷新验证用户名是否存在 (使用AjaxPro.2.dll)(当鼠标离开输入框时,即使判断用户名是否存在)
(AjaxPro.2.dll学习参考:http://www.cnblogs.com/lexus/archive/2007/11/29/977281.html)
1. 将AjaxPro.2.dll添加引用
2. 在web.Config文件中<system.web>节点中添加
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
3. 添加一个新页面Infopulish.aspx
在Infopulish.aspx.cs中
using AjaxPro;
public partial class croInfPulish : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(croInfPulish), this); //注册当前页面类名
}
}
#region "构造前台JS调用的服务器方法,必须声明[AjaxPro.AjaxMethod]才可被前台Js调用"
[AjaxPro.AjaxMethod]
public bool CheckCcoode(string code) //判断用户名是否存在
{
bool flag = false;
flag = CraneInfoManager.GetCountBool(code);
return flag;
}
#endregion
}
在Infopulish.aspx中
<script type="text/javascript">
function validate() {
var num = document.getElementById("txtcode").value; //获得输入值
if (num == "") {
alert("该编号不允许为空,请填写");
document.getElementById("txtcode").focus();
return;
}
else {
var flag = Infopulish.CheckCcoode(num).value; //调用后台.cs文件(即服务器方法)中的CheckCcoode()方法(如果该页面是在命名空间a下的,则此处应改为a.Infopulish.CheckCcoode(num).value)
if (flag) {
document.getElementById("msg").innerHTML = "<font color='red'>该编号可用</font>";
}
else {
document.getElementById("msg").innerHTML = "<font color='red'><br/>该编号已存在,请重新填写</font>";
document.getElementById("txtcode").focus();
return;
}
}
}
</script>
<body>
<form id="form1" runat="server">
<input id="txtcode" type="text" runat="server" οnblur="validate()" />
</form>
</body>
Ajax实现无刷新验证用户名是否存在 (使用AjaxPro.2.dll)(当鼠标离开输入框时,即使判断用户名是否存在)
最新推荐文章于 2019-09-08 16:12:38 发布