基于Ajax技術(shù)實(shí)現(xiàn)無(wú)刷新用戶登錄功能
代碼如下:
// JScript 文件 function usersLogon() { var userName = document.getElementById("txtuserName").value; var password = document.getElementById("txtpassword").value; var checkCode = document.getElementById("txtCheckCode").value; var response = userControl_logon.CheckCodeIsRight(checkCode).value; if(userName == "") { document.getElementById("txtuserName").focus(); return false; } else if(password == "") { document.getElementById("txtpassword").focus(); return false; } else if(checkCode =="") { document.getElementById("txtCheckCode").focus(); return false; } else { if(response == true) { //判斷用戶是否存在 userControl_logon.userNameAndPasswordIsExist(userName,password,userNameIsRight); } else { alert("驗(yàn)證碼出錯(cuò)"); userControl_logon.checkCodeOperaotr(refreshCheckCode); document.getElementById("txtpassword").value = ""; } } } function userNameIsRight(res) { var userName = document.getElementById("txtuserName").value; if(res.value == true) { //用戶存在,但要看此用戶有沒有進(jìn)入管理留言版權(quán)限, userControl_logon.userNameIsRight(userName,CallBack); } else { alert("用戶名或密碼錯(cuò)誤"); document.getElementById("txtpassword").value = ""; OnLoad_checkCode(); } } function CallBack(res) { if(res.value == true) { hideLogon(); var url = userControl_logon.returnUrl(); if ( url.value == 404) { showDefault(); } else { document.getElementById("Url").innerHTML = '<a href="' + url.value + '">' + url.value + '</a>' } } else { alert("對(duì)不起你的權(quán)限不夠"); document.getElementById("txtpassword").value = ""; OnLoad_checkCode(); } } //隱藏登錄框 function hideLogon() { var element = document.getElementById("hideLogon") element.style.display = "none" } //顯示返回首頁(yè) function showDefault() { var element = document.getElementById("Returndefault") element.style.display = "block" } function OnLoad_checkCode() { userControl_logon.checkCodeOperaotr(refreshCheckCode); document.getElementById("txtuserName").focus(); // return false; } ///重新得到新的驗(yàn)證嗎 function refreshCheckCode(res) { document.getElementById("txtCheckCode").value = ""; document.getElementById("lblNumber").innerHTML = res.value; } function abce() { alert(document.getElementById("lblNumber").value) }
下面代碼
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using LHB_SQL_2005; public partial class userControl_logon : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { AjaxPro.Utility.RegisterTypeForAjax(typeof(userControl_logon)); } } [AjaxPro.AjaxMethod] public static string checkCodeOperaotr() { string _checkCode = GeneralMethod.GenerateCheckCode(); System.Web.HttpContext.Current.Session["checkCode"] = _checkCode; //返回驗(yàn)證碼 return _checkCode; } /// <summary> /// 判斷驗(yàn)證是否正確 /// </summary> /// <param name="checkCode"></param> /// <returns></returns> [AjaxPro.AjaxMethod] public static bool CheckCodeIsRight(string checkCode) { string _checkCode = (string)(System.Web.HttpContext.Current.Session["checkCode"]); if (_checkCode == checkCode) { return true; } else { return false; } } /// <summary> /// 判斷用戶名及密碼添加是否正確 /// </summary> /// <param name="userName">用戶名</param> /// <param name="_password">用戶名密碼</param> /// <returns>bool</returns> [AjaxPro.AjaxMethod] public static bool userNameAndPasswordIsExist(string userName, string _password) { string password = GeneralMethod.ToEncryptPassword(_password); string executeString = "SELECT COUNT(*) FROM users WHERE userName = '" + userName.ToString() + "' AND password = '" + password + "'"; int count = int.Parse(GetCommand.ExecuteScalar(executeString)); if (count == 1) { System.Web.HttpContext.Current.Session["userName"] = userName; return true; } else { return false; } } /// <summary> /// 判斷用戶是不是有這進(jìn)入管理留言版的權(quán)限 /// </summary> /// <param name="userName">用戶名</param> /// <returns></returns> [AjaxPro.AjaxMethod] public static bool userNameIsRight(string userName) { string executeString = "SELECT [right] FROM role WHERE usersId = (select userNameId from users where userName = '" + userName + "')"; int count = int.Parse(GetCommand.ExecuteScalar(executeString)); if (count > 0) { return true; } else { return false; } } /// <summary> /// 返回Url值 /// </summary> /// <returns></returns> [AjaxPro.AjaxMethod] public static string returnUrl() { string url = ""; try { url = System.Web.HttpContext.Current.Session["url"].ToString(); } catch { url ="404"; } return url; } }
下面是頁(yè)面代碼
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="logon.ascx.cs" Inherits="userControl_logon" %> <script language="javascript" type="text/javascript" src="../JavaScript/logon.js"> </script> <script language="javascript" type="text/javascript" src="JavaScript/logon.js"> </script> <link href="../CSS/table_css.css" rel="stylesheet" type="text/css" /> <link href="CSS/table_css.css" rel="stylesheet" type="text/css" /> <body onload="OnLoad_checkCode();"> <div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table id="hideLogon" border="0" cellpadding="0" cellspacing="0" style="display:block;"> <tr> <td style="background-color: #99ccff">用戶名:</td> <td><input type="text" id="txtuserName" style="width: 105px" /></td> </tr> <tr> <td style="background-color: #99ccff">密 碼:</td> <td> <input id="txtpassword" type="password" style="width: 105px" /></td> </tr> <tr> <td style="background-color: #99ccff">驗(yàn)證碼:</td> <td style="background-color: #99ccff"> <input type= "text" id="txtCheckCode" style=" width:60px" /><label id="lblNumber"></label></td> </tr> <tr> <td style="background-color: #99ccff"></td> <td style="background-color: #99ccff"> <input type="button" onclick="usersLogon();" value="登錄" id="btnLogon" /></td> </tr> </table> </td> </tr> <tr> <td > <div id="Url"></div> </td> </tr> <tr> <td align="center"> <table id="Returndefault" border="0" cellpadding="0" cellspacing="0" style="display:none;"> <tr> <td> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">返回首頁(yè)</asp:HyperLink></td> </tr> </table> </td> </tr> </table> </div> </body>
以上所述是小編給大家介紹的基于Ajax技術(shù)實(shí)現(xiàn)無(wú)刷新用戶登錄功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于多個(gè)Ajax請(qǐng)求執(zhí)行返回先后的問(wèn)題示例探討
這篇文章主要與大家探討下關(guān)于多個(gè)Ajax請(qǐng)求執(zhí)行返回先后的問(wèn)題,需要的朋友可以參考下2014-07-07ajax三級(jí)聯(lián)動(dòng)下拉菜單效果
這篇文章主要為大家詳細(xì)介紹了ajax三級(jí)聯(lián)動(dòng)下拉菜單的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Ajax與mysql數(shù)據(jù)交互實(shí)現(xiàn)留言板功能
最近自己做了一個(gè)小demo,實(shí)現(xiàn)了Ajax與MySQL的數(shù)據(jù)交互,js部分用的是jq,后臺(tái)用的是PHP,數(shù)據(jù)庫(kù)是mysql,下面通過(guò)本文給大家詳細(xì)介紹下2016-12-12Ajax 動(dòng)態(tài)載入html頁(yè)面后不能執(zhí)行其中的js快速解決方法
這篇文章主要介紹了Ajax 動(dòng)態(tài)載入html頁(yè)面后不能執(zhí)行其中的js快速解決方法,需要的朋友可以參考下2018-06-06AJAX +SpringMVC 實(shí)現(xiàn)bootstrap模態(tài)框的分頁(yè)查詢功能
這篇文章主要介紹了AJAX +SpringMVC 實(shí)現(xiàn)bootstrap模態(tài)框的分頁(yè)查詢功能,以及模態(tài)框 ajax分頁(yè)實(shí)例代碼分享,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01