Implement the function of logging on to users without refreshing new users based on Ajax technology.
The Code is as follows:
// JScript file 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) {// determine whether the user has userControl_logon.userNameAndPasswordIsExist (userName, password, userNameIsRight );} else {alert ("Verification code error"); userControl_logon.checkCodeOperaotr (refreshCheckCode); document. getElementById ("txtpassword "). value = "" ;}} function userNameIsRight (res) {var userName = document. getElementById ("txtuserName "). value; if (res. value = true) {// The user exists, but it depends on whether the user has the permission to manage the message board. userControl_logon.userNameIsRight (userName, CallBack );} else {alert ("incorrect user name or password"); 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 ("sorry, you are not authorized"); document. getElementById ("txtpassword "). value = ""; OnLoad_checkCode () ;}// hide the logon box function hideLogon () {var element = document. getElementById ("hideLogon") element. style. display = "none"} // display the returned home page function showDefault () {var element = document. getElementById ("Returndefault") element. style. display = "block"} function OnLoad_checkCode () {userControl_logon.checkCodeOperaotr (refreshCheckCode); document. getElementById ("txtuserName "). focus (); // return false;} // is the function refreshCheckCode (res) {document. getElementById ("txtCheckCode "). value = ""; document. getElementById ("lblNumber "). innerHTML = res. value;} function abce () {alert (document. getElementById ("lblNumber "). value )}
The following code
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, E VentArgs 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; // return the verification code return _ checkCode ;} /// <summary> /// check whether the verification is correct /// </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> /// determine whether the user name and password are correctly added // </summary> /// <param name = "userName"> User name </param>/ // <param name = "_ password"> username and 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> /// determine whether the user has the permission to enter the Management message board. /// </summary> /// <param name = "userName"> User name </ 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> /// return Url value /// </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 ;}}
Below is the page code
<% @ 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: #99 ccff "> User Name: </td> <input type = "text" id = "txtuserName" style = "width: 105px "/> </td> </tr> <td style =" background-color: #99 ccff "> password: </td> <input Id = "txtpassword" type = "password" style = "width: 105px"/> </td> </tr> <td style = "background-color: #99 ccff "> Verification Code: </td> <td style =" background-color: #99 ccff "> <input type =" text "id =" txtCheckCode "style =" width: 60px "/> <label id =" lblNumber "> </label> </td> </tr> <td style =" background-color: #99 ccff "> </td> <td style =" background-color: #99 ccff "> <input type =" button "onclick =" usersLogon (); "valu E = "login" id = "btnLogon"/> </td> </tr> </table> </td> </tr> <td> <div id = "Url"> </div> </td> </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 "> back to homepage </asp: hyperLink> </td> </tr> </table> </div> </body>
The above section describes how to use Ajax technology to implement the function of logging on to a non-refreshing user. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!