问题描述
我想实现一个有关利用ajax(js)判断用户名是否被占用的功能,不知道谁能够帮忙解决一下<inputid="tbUsername"type="text"runat="server"class="username"style="width:180px;"onfocus="setStyle1('LbUserName')"onblur="setStyle2('LbUserName',this.id)"/>所用到的js为<scripttype="text/javascript">functionsetStyle1(x){//document.getElementById("LbUserName").Visible="false"document.getElementById(x).style.display='none'}functionsetStyle2(x,y){if(document.getElementById(y).value==""){document.getElementById(x).innerHTML='请输入用户名'document.getElementById(x).style.display=''document.getElementById(x).style.borderColor="#00A8FF"document.getElementById(x).style.color='blue'document.getElementById(x).style.backgroundColor="#e2f5ff"}else{CheckUserName(document.getElementById('y').value)//document.getElementById(x).innerHTML='填写正确'//document.getElementById(x).style.display=''//document.getElementById(x).style.borderColor="#81A235"//document.getElementById(x).style.color='#5A8F00'//document.getElementById(x).style.backgroundColor="white"}}varxmlHttp;//创建CheckUserNamefunctionCheckUserName(name){createXmlHttpRequest();varurl="DisposeEvent.aspx?Name="+name+"&Event=Check";;xmlHttp.open("post",url,true);xmlHttp.onreadystatechange=CheckUserNameResult;xmlHttp.send(null);}functioncreateXmlHttpRequest(){if(window.XMLHttpRequest){xmlHttp=newXMLHttpRequest();if(xmlHttp.overrideMimeType){xmlHttp.overrideMimeType("text/xml");}}elseif(window.ActiveXObject){try{xmlHttp=newActiveXObject("Msxml2.XMLHTTP");}catch(e){xmlHttp=newActiveXObject("Microsoft.XMLHTTP");}}if(!xmlHttp){window.alert("你的浏览器不支持创建XMLhttpRequest对象");}returnxmlHttp;}//创建用户检测的回调函数functionCheckUserNameResult(){if(xmlHttp.readyState==4)//服务器响应状态{if(xmlHttp.status==200)//代码执行状态{if(xmlHttp.responseText=="false"){document.getElementById("imgflag").innerHTML='该用户名可用'document.getElementById(x).style.display=''document.getElementById(x).style.borderColor="#FF6600"document.getElementById(x).style.color='black'document.getElementById(x).style.backgroundColor="#FFF2E9"}else{document.getElementById("imgflag").innerHTML='该用户名已经存在'document.getElementById(x).style.display=''document.getElementById(x).style.borderColor="#FF6600"document.getElementById(x).style.color='black'document.getElementById(x).style.backgroundColor="#FFF2E9"}}}}</script>DisposeEvent.aspx.cs代码为protectedvoidPage_Load(objectsender,EventArgse){CheckUsercu=newCheckUser();cu.UserName=Request.QueryString["Name"].ToString();if(!IsPostBack){switch(Request.QueryString["Event"]){case"Check":if(cu.CheckUsers()){Response.Write("true");Response.End();}else{Response.Write("false");Response.End();}break;}其中CheckUser书一个类代码为publicclassCheckUser{privatestring_UserName;publicstringUserName{set{this._UserName=value;}get{returnthis._UserName;}}publicboolCheckUsers(){SqlParameter[]Param=newSqlParameter[1];DataBaseDB=newDataBase();Param[0]=DB.MakeInParam("@UserName",SqlDbType.VarChar,50,this.UserName);if(DB.GetDataSet("USP_CheckUsers",Param).Tables[0].Rows.Count>0){returntrue;}else{returnfalse;}}}不知道那里错了
解决方案
解决方案二:
//声明XMLHttpRequest对象varxmlHttp;//检测用户名是否存在functionCheckName(userName){createXMLHTTP();//创建XMLHttpRequest对象varurl="DisposeEvent.aspx?userName="+userName+"&Event=Check";xmlHttp.open("GET",url,true);xmlHttp.onreadystatechange=checkUserName;xmlHttp.send(null);}functioncreateXMLHTTP(){if(window.XMLHttpRequest){xmlHttp=newXMLHttpRequest();//mozilla浏览器}elseif(window.ActiveXObject){try{xmlHttp=newActiveXObject("Msxml2.XMLHTTP");//IE老版本}catch(e){}try{xmlHttp=newActiveXObject("Microsoft.XMLHTTP");//IE新版本}catch(e){}if(!xmlHttp){window.alert("不能创建XMLHttpRequest对象实例!");returnfalse;}}}//执行检测用户名回调函数functioncheckUserName(){if(xmlHttp.readyState==4)//判断对象状态{if(xmlHttp.status==200)//信息成功返回,开始处理信息{if(xmlHttp.responseText=="true"){document.getElementById("imgName").src="images/true.gif";//让注册按钮失效document.getElementById("btnReg").disabled=false;}else{document.getElementById("imgName").src="images/false.gif";document.getElementById("btnReg").disabled=true;}}}}
解决方案三:
publicpartialclassDisposeEvent:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){Useruser=newUser();//是否为执行CheckName方法if(Request.QueryString["Event"].ToString()=="Check"){if(user.checkName(Request.QueryString["userName"].ToString())){//当数据库中已存在此用户时输出为false,让其显示相应图标Response.Write("false");Response.End();}else{Response.Write("true");Response.End();}}}}
解决方案四:
publicclassUser{publicUser(){////TODO:在此处添加构造函数逻辑//}//检测用户名是否存在stringconStr=System.Configuration.ConfigurationSettings.AppSettings["HumanResources"];publicboolcheckName(stringuserName){SqlConnectionuserConnection=newSqlConnection(conStr);SqlCommanduserCommand=newSqlCommand("checkName",userConnection);userCommand.CommandType=CommandType.StoredProcedure;//采用存储过程userCommand.Parameters.Add("@userName",SqlDbType.VarChar,50);//存储过程参数userCommand.Parameters["@userName"].Value=userName;//给参数赋值userCommand.Parameters.Add("@count",SqlDbType.Int);userCommand.Parameters["@count"].Direction=ParameterDirection.Output;userCommand.Connection.Open();//打开连接userCommand.ExecuteScalar();//返回首行首列,如果存在的话返回1intn=Convert.ToInt32(userCommand.Parameters["@count"].Value);userCommand.Connection.Close();//关闭连接if(n>0){returntrue;}else{returnfalse;}}}
解决方案五:
http://topic.csdn.net/u/20080616/08/7d35368b-f257-4920-8f27-e0ff474e14f5.html==这里我不是回过么
解决方案六:
在你自己的input的中加<inputid="userName"type="text"onkeyup="CheckName(document.getElementById('userName').value);"/>