用ajax技术实现 无刷新实现用户登录
发布时间:2020-12-16 00:50:45 所属栏目:百科 来源:网络整理
导读:代码如下: // JScript文件 function usersLogon() { var userName=document.getElementById("txtuserName").value; var password=document.getElementById("txtpassword").value; var checkCode=document.getElementById("txtCheckCode").value; var respons
代码如下:
//
JScript文件
下而后面代码
functionusersLogon() { varuserName=document.getElementById("txtuserName").value; varpassword=document.getElementById("txtpassword").value; varcheckCode=document.getElementById("txtCheckCode").value; varresponse=userControl_logon.CheckCodeIsRight(checkCode).value; if(userName=="") { document.getElementById("txtuserName").focus(); returnfalse; } elseif(password=="") { document.getElementById("txtpassword").focus(); if(checkCode=="") { document.getElementById("txtCheckCode").focus(); else { if(response==true) { //判断用户是否存在 userControl_logon.userNameAndPasswordIsExist(userName,password,userNameIsRight); } else { alert("验证码出错"); userControl_logon.checkCodeOperaotr(refreshCheckCode); document.getElementById("txtpassword").value=""; } } } functionuserNameIsRight(res) { if(res.value==true) { 用户存在,但要看此用户有没有进入管理留言版权限, userControl_logon.userNameIsRight(userName,CallBack); } else { alert("用户名或密码错误"); document.getElementById("txtpassword").value=""; OnLoad_checkCode(); } } functionCallBack(res) { true) { hideLogon(); varurl=userControl_logon.returnUrl(); if(url.value==404) { showDefault(); } else { document.getElementById("Url").innerHTML='<ahref="'+url.value+'">'+url.value+'</a>' } } else { alert("对不起你的权限不够"); document.getElementById("txtpassword").value=""; OnLoad_checkCode(); } } 隐藏登录框 functionhideLogon() { varelement=document.getElementById("hideLogon") element.style.display="none" } 显示返回首页 functionshowDefault() { varelement=document.getElementById("Returndefault") element.style.display="block" } functionOnLoad_checkCode() { userControl_logon.checkCodeOperaotr(refreshCheckCode); document.getElementById("txtuserName").focus(); returnfalse; } /重新得到新的验证吗 functionrefreshCheckCode(res) { document.getElementById("txtCheckCode").value=""; document.getElementById("lblNumber").innerHTML=res.value; } functionabce() { alert(document.getElementById("lblNumber").value) } usingSystem;
usingSystem.Data; usingSystem.Configuration; usingSystem.Collections; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Web.UI.HtmlControls; usingSystem.Drawing; usingLHB_SQL_2005; public partial classuserControl_logon:System.Web.UI.UserControl { protectedvoidPage_Load(objectsender,EventArgse) { if(!this.IsPostBack) { AjaxPro.Utility.RegisterTypeForAjax(typeof(userControl_logon)); } } [AjaxPro.AjaxMethod] publicstaticstringcheckCodeOperaotr() { string_checkCode=GeneralMethod.GenerateCheckCode(); System.Web.HttpContext.Current.Session["checkCode"]=_checkCode; 返回验证码 return_checkCode; } ///<summary> 判断验证是否正确 </summary><paramname="checkCode"></param><returns></returns> [AjaxPro.AjaxMethod] boolCheckCodeIsRight(stringcheckCode) { string_checkCode=(string)(System.Web.HttpContext.Current.Session["checkCode"]); if(_checkCode==checkCode) { true; } else { false; } } 判断用户名及密码添加是否正确 <paramname="userName">用户名</param><paramname="_password">用户名密码<returns>bool</returns>booluserNameAndPasswordIsExist(stringuserName,string_password) { stringpassword=GeneralMethod.ToEncryptPassword(_password); stringexecuteString="SELECTCOUNT(*)FROMusersWHEREuserName='"+userName.ToString()+"'ANDpassword='"+password+"'"; intcount=int.Parse(GetCommand.ExecuteScalar(executeString)); if(count==1) { System.Web.HttpContext.Current.Session["userName"]=userName; else { false; } } 判断用户是不是有这进入管理留言版的权限 booluserNameIsRight(stringuserName) { stringexecuteString="SELECT[right]FROMroleWHEREusersId=(selectuserNameIdfromuserswhereuserName='"+userName+"')"; if(count>0) { else { false; } } 返回Url值 stringreturnUrl() { stringurl=""; try { url=System.Web.HttpContext.Current.Session["url"].ToString(); } catch { url="404"; } returnurl; } } 下面是页面代码
<%
@ControlLanguage="C#AutoEventWireuptrueCodeFilelogon.ascx.csInheritsuserControl_logon
%>
< script language ="javascript" type ="text/javascript" src ="../JavaScript/logon.js" > </ script > ="JavaScript/logon.js" link href ="../CSS/table_css.css" rel ="stylesheet" ="text/css" /> ="CSS/table_css.css" body onload ="OnLoad_checkCode();" div table border ="0" cellpadding cellspacing ="0" tr td id ="hideLogon" style ="display:block;" td ="background-color:#99ccff" >用户名: >< input ="text" ="txtuserName" ="width:105px" /></ >密码: ="txtpassword" ="password" >验证码: ="txtCheckCode" ="width:60px" />< label ="lblNumber" ></ label ="button" onclick ="usersLogon();" value ="登录" ="btnLogon" table div ="Url" align ="center" ="Returndefault" ="display:none;" asp:HyperLink ID ="HyperLink1" runat ="server" NavigateUrl ="~/Default.aspx" >返回首页 asp:HyperLink body > (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |