RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
一款经典的ajax登录页面后台asp.net

实现过程

  1. 新建一名为login.htm的静态网页文件,作为登录页面,如图

  body标签代码,代码如下:

 
 
 
 
  1.    
  2.  
  3.  
  4.  
  5. valign="middle"> 
  6. 用户名: td> 
  7.  
  8. td> 
  9. valign="middle"> 
  10. span> td> 
  11. tr> 
  12.  
  13. valign="middle"> 
  14. 密码: td> 
  15.  
  16. td> 
  17. valign="middle"> span> 
  18. td> 
  19. tr> 
  20.  
  21.  
  22. 记住密码一个月 td> 
  23. tr> 
  24.  
  25.  
  26.  
  27. td> 
  28. tr> 
  29. table> 
  30. div> 
  31. body>

#p#

  2. 在login.htm中引入外部js代码

 
 
 
 
  1. script>
  2. script>

  其中aj.js为ajax封装的类,loginCookie.js为对Cookie操作的代码

  aj.js代码如下:

 
 
 
 
  1. //JScript文件
  2. //ajax请求功能函数
  3. //get调用方式:(1)实例化 var aj=new ajax(); (2)调用get函数 aj.get(url,callback) (3)写回调函数 function callback(xhr){xhr.responsetext}
  4. //post调用方式:(1)实例化 var aj=new ajax(); (2)调用post函数 aj.post(url,content,callback) (3)写回调函数 function callback(xhr){xhr.responsetext}
  5. //构造ajax对象
  6. function ajax() 
  7. {
  8. function getXHR()//获取xmlhttprequest
  9. {
  10. var request=false;
  11. try 
  12. {
  13. request = new XMLHttpRequest();
  14. catch (trymicrosoft) 
  15. {
  16. try 
  17. {
  18. request = new ActiveXObject("Msxml2.XMLHTTP");
  19. catch (othermicrosoft) 
  20. {
  21. try 
  22. {
  23. request = new ActiveXObject("Microsoft.XMLHTTP");
  24. catch (failed) 
  25. {
  26. request = false;
  27. }
  28. }
  29. }
  30. return request;
  31. }
  32. this.get = function (openUrl,successFun)//ajax对象的get方法,通过get方式发送请求,openUrl为请求的页面,successFun为成功回调执行的函数
  33. {
  34. var request = getXHR();
  35. request.open("get",openUrl,true);
  36. // request.onreadystatechange = function ()
  37. // {
  38. // if (request.readystate==4)
  39. // {
  40. // if (request.status==200)
  41. // {
  42. // successFun(request);
  43. // }
  44. // }
  45. // };
  46. request.onreadystatechange = update;
  47. function update()
  48. {
  49. if (request.readystate==4)
  50. {
  51. if (request.status==200)
  52. {
  53. successFun(request);
  54. }
  55. }
  56. }
  57. request.send(null);
  58. }
  59. this.post = function (openUrl,sendContent,successFun)//ajax对象的post方法,通过post方式发送请求,openUrl为请求的页面,successFun为成功回调执行的函数
  60. {
  61. var request = getXHR();
  62. request.open("post",openUrl,true);
  63. request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//告诉服务器发送的是文本
  64. request.onreadystatechange = update;
  65. function update()
  66. {
  67. if (request.readystate==4)
  68. {
  69. if (request.status==200)
  70. {
  71. successFun(request);
  72. }
  73. }
  74. }
  75. request.send(sendContent);
  76. }
  77. }

  loginCookie.js代码如下

 
 
 
 
  1. //JScript文件
  2. //SetCookie 保存一个Cookie。参数中除了name和value以外,其他可以省略。
  3. //GetCookie 通过一个Cookie的名字取出它的值。
  4. //DelCookie 删除一个Cookie,也就是让一个Cookie立刻过期。参数中除了name,其他可以省略。
  5. //测试
  6. //SetCookie("username", "123");expires代表"月"
  7. //alert(GetCookie("username"));
  8. //DelCookie("username");
  9. //alert(GetCookie("username"));
  10. function SetCookie(name, value, expires, path, domain, secure) {
  11. var today = new Date();
  12. today.setTime(today.getTime());
  13. if(expires) { expires *= 2592000; }
  14. var expires_date = new Date(today.getTime() + (expires));
  15. document.cookie = name + "=" + escape(value)
  16. + (expires ? ";expires=" + expires_date.toGMTString() : "")
  17. + (path ? ";path=" + path : "")
  18. + (domain ? ";domain=" + domain : "")
  19. + (secure ? ";secure" : "");
  20. }
  21. function GetCookie(name) {
  22. var cookies = document.cookie.split( ';' );
  23. var cookie = '';
  24. for(var i=0; i
  25. cookie = cookies[i].split('=');
  26. if(cookie[0].replace(/^\s+|\s+$/g, '') == name) {
  27. return (cookie.length <= 1) ? "" : unescape(cookie[1].replace(/^\s+|\s+$/g, ''));
  28. }
  29. }
  30. return null;
  31. }
  32. function Delcookie(name, path, domain) {
  33. document.cookie = name + "="
  34. + (path ? ";path=" + path : "")
  35. + (domain ? ";domain=" + domain : "")
  36. + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  37. }

#p#

  3. 写login.htm页面中的js代码,放在head标签之间

 
 
 
 
  1. window.onload = function (){
  2. document.getElementById ('txtusername').focus();//用户名框获得焦点
  3. if (GetCookie('user_name') != null && GetCookie('user_pwd') != null)//设置记住密码的登录页面
  4. {
  5. document.getElementById ("txtusername").value = GetCookie('user_name');
  6. document.getElementById ("txtpwd").value = GetCookie('user_pwd');
  7. }
  8. }
  9. String.prototype.Trim = function() //自定义的去除字符串两边空格的方法
  10. return this.replace(/(^\s*)|(\s*$)/g, ""); 
  11. function checkuser()//检验用户名是否正确
  12. {
  13. var img = document.getElementById ("imgCheck")
  14. img.src="iamges/blue-loading.gif";//设置图片及其可见性
  15. img.style.visibility = "visible";
  16. var aj = new ajax();//以下为ajax请求
  17. var username = document.getElementById ("txtusername").value.Trim();
  18. var url = "login.aspx?uname="+escape(username);
  19. aj.get(url,callback);
  20. function callback(obj)
  21. {
  22. var response = obj.responsetext;
  23. var res = response.split('\n');
  24. if (res[0] == "ok")
  25. {
  26. img.src="iamges/icon-info.gif";
  27. document.getElementById ("unMessage").innerHTML = " 用户名正确" ;  
  28. }
  29. else
  30. {
  31. img.src="iamges/icon-warning.gif";
  32. document.getElementById ("unMessage").innerHTML = " 用户名错误" ;  
  33. }
  34. }
  35. }
  36. function login()//登录
  37. {
  38. if (document.getElementById ("unMessage").innerText == "用户名错误")
  39. {
  40. alert("你的用户名错误");
  41. }
  42. else if (document.getElementById ("txtpwd").value == "")
  43. {
  44. alert("请输入密码");
  45. }
  46. else
  47. {
  48. var aj = new ajax();
  49. var username = document.getElementById ("txtusername").value.Trim();
  50. var userpwd = document.getElementById ("txtpwd").value;
  51. var url = "login.aspx?name="+escape(username)+"&pwd="+escape(userpwd);
  52. aj.get(url,callback);
  53. function callback(obj)
  54. {
  55. var response = obj.responsetext;
  56. var res = response.split('\n');
  57. if (res[0] == "ok")
  58. {
  59. if (document.getElementById ("cbRememberPwd").checked)
  60. {
  61. SetCookie('user_name',username,1);//保存密码一个月
  62. SetCookie('user_pwd',userpwd,1);
  63. }
  64. else
  65. {
  66. SetCookie('user_name',username);
  67. SetCookie('user_pwd',userpwd);
  68. }
  69. window.open ("loginIndex.htm","_self");
  70. }
  71. else
  72. &p; {
  73. alert("密码错误");
  74. }
  75. }
  76. }
  77. }
  78. function reset()//重置
  79. {
  80. window.onload();//执行窗体登录事件
  81. document.getElementById ("txtusername").value="";
  82. document.getElementById ("txtpwd").value="";
  83. }
  84. function enterLogin()
  85. {
  86. if (event.keyCode==13) //如果按下的是Enter键的话,就执行登录语句
  87. {
  88. login();
  89. }
  90. }
  91. script>

#p#

  4. 新建一名为login.aspx的页面,该页面作为ajax请求的页面,login.aspx.cs代码如下

 
 
 
 
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. OleDbConnection Conn = DBcon.get_con();
  4. if (Request["uname"] != null)
  5. {
  6. string username = Request["uname"].ToString();
  7. string strSql = "select * from [user] where u_name='" + username + "'";
  8. Conn.Open();
  9. OleDbCommand Comd = new OleDbCommand(strSql, Conn);
  10. OleDbDataReader dr = Comd.ExecuteReader();
  11. if (dr.Read())
  12. {
  13. Response.Write("ok\n");
  14. }
  15. else
  16. {
  17. Response.Write("fail\n");
  18. }
  19. //if (Comd.ExecuteNonQuery() > 0)
  20. //{
  21. // Response.Write("存在这个用户\n");
  22. //}
  23. //else
  24. //{
  25. // Response.Write("没有此用户名\n");
  26. //}
  27. Conn.Close();
  28. }
  29. if (Request["name"] != null && Request["pwd"] != null)
  30. {
  31. string name = Request["name"].ToString();
  32. string pwd = Request["pwd"].ToString();
  33. string strSql = "select * from [user] where u_name='" + name + "'" + " and u_pwd='" + pwd + "'";
  34. Conn.Open();
  35. OleDbCommand Comd = new OleDbCommand(strSql, Conn);
  36. OleDbDataReader dr = Comd.ExecuteReader();
  37. if (dr.Read())
  38. {
  39. Response.Write("ok\n");
  40. }
  41. else
  42. {
  43. Response.Write("fail\n");
  44. }
  45. }
  46. }

  5. 新建一名为loginIndex.htm的静态页面,作为用户登录之后的首页

  其body标签代码如下:

 
 
 
 
  1.   span>
  2. body>

  6. 在loginIndex.htm页面引入loginCookie.js文件

 
 
 
 
  1. script>

  7. 写loginIdex.htm页面的js代码,放在head标签之间

 
 
 
 
  1. window.onload = function ()
  2. {
  3. if(GetCookie('user_name')==null || GetCookie('user_pwd')==null)//如果没有登录,而是直接在网页中输入网址的
  4. {
  5. alert('你还没有登录!\n返回到登陆页面。');
  6. window.navigate("login.htm");
  7. }
  8. else
  9. {
  10. var uname = GetCookie('user_name');
  11. document.getElementById ('username').innerHTML ="欢迎你: " + uname + " 注销 a>";//提供"注销"按钮
  12. }
  13. }
  14. function off()//注销事件
  15. {
  16. if (window.confirm("你真要注销吗?"))
  17. {
  18. Delcookie("user_name");
  19. Delcookie("user_pwd");
  20. window.navigate("login.htm");
  21. }
  22. }
  23. script>

#p#

  8. 完成并演示

  客户端代码较多,但是交互性很好,演示如下:

  当输入完用户名,鼠标光标离开用户名框之后,系统会快速检验用户名是否合法

图-演示1

图-演示2

  进入首页后,出现的窗口,带有当前登录的用户和注销按钮

图-演示3

  当用户点击注销按钮时,会友情提示你是否真的注销

图-演示4

  当你不是输入用户和密码登陆,也是直接在浏览器地址栏中输入首页网址的时候,系统会提示你没有登录,并直接返回到登陆页面。

图-演示5

  当用户输入了有效的用户名和密码,并要求系统记住密码,用户下次进入到登录页面时,系统会把上次记住的用户名和密码显示在输入框中。。

  并且这个时候直接在浏览器的地址栏中输入首页地址,也是能正常访问的。

图-演示7


当前题目:一款经典的ajax登录页面后台asp.net
URL分享:http://www.jxjierui.cn/article/cdosooo.html