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. } 
  15. catch (trymicrosoft) 
  16. {
  17. try 
  18. {
  19. request = new ActiveXObject("Msxml2.XMLHTTP");
  20. } 
  21. catch (othermicrosoft) 
  22. {
  23. try 
  24. {
  25. request = new ActiveXObject("Microsoft.XMLHTTP");
  26. } 
  27. catch (failed) 
  28. {
  29. request = false;
  30. }
  31. }
  32. }
  33. return request;
  34. }
  35. this.get = function (openUrl,successFun)//ajax对象的get方法,通过get方式发送请求,openUrl为请求的页面,successFun为成功回调执行的函数
  36. {
  37. var request = getXHR();
  38. request.open("get",openUrl,true);
  39. // request.onreadystatechange = function ()
  40. // {
  41. // if (request.readystate==4)
  42. // {
  43. // if (request.status==200)
  44. // {
  45. // successFun(request);
  46. // }
  47. // }
  48. // };
  49. request.onreadystatechange = update;
  50. function update()
  51. {
  52. if (request.readystate==4)
  53. {
  54. if (request.status==200)
  55. {
  56. successFun(request);
  57. }
  58. }
  59. }
  60. request.send(null);
  61. }
  62. this.post = function (openUrl,sendContent,successFun)//ajax对象的post方法,通过post方式发送请求,openUrl为请求的页面,successFun为成功回调执行的函数
  63. {
  64. var request = getXHR();
  65. request.open("post",openUrl,true);
  66. request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//告诉服务器发送的是文本
  67. request.onreadystatechange = update;
  68. function update()
  69. {
  70. if (request.readystate==4)
  71. {
  72. if (request.status==200)
  73. {
  74. successFun(request);
  75. }
  76. }
  77. }
  78. request.send(sendContent);
  79. }
  80. }

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