当前位置:Gxlcms > asp.net > Ajax+asp.net实现用户登陆

Ajax+asp.net实现用户登陆

时间:2021-07-01 10:21:17 帮助过:24人阅读

以用户登录为例练习ajax的使用方法

login.html

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title></title>
  6. <script type="text/javascript">
  7. var obj = createobj();
  8. function login(name, pwd)
  9. {
  10. var urlstr = "http://localhost:14248/server.aspx?username=" + name + "&password=" + pwd;
  11. obj.open("get", urlstr, true);
  12. obj.onreadystatechange = dowork;
  13. obj.send();
  14. }
  15. function dowork()
  16. {
  17. if (obj.readyState == 4)
  18. {
  19. if (obj.status == 200)
  20. {
  21. document.getElementById("msg").innerText = obj.responseText;
  22. }
  23. }
  24. }
  25. //创建对象
  26. function createobj()
  27. {
  28. var xmlHttp = null;
  29. try {
  30. //非IE浏览器
  31. xmlHttp = new XMLHttpRequest();
  32. }
  33. catch (e)
  34. { //IE浏览器
  35. try{
  36. xmlHttp = new ActiveXObject("Msxml2.HTTP");
  37. }
  38. catch (e)
  39. {
  40. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  41. }
  42. }
  43. return xmlHttp;
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. <table>
  49. <tr>
  50. <td align="center" colspan="2">登录</td>
  51. </tr>
  52. <tr>
  53. <td>用户名:</td>
  54. <td><input type="text" id="username" name="username" /></td>
  55. </tr>
  56. <tr>
  57. <td>密码:</td>
  58. <td><input type="password" id="password" name="password" /></td>
  59. </tr>
  60. <tr>
  61. <td >
  62. <input type="submit" value="登录" onclick="login(document.getElementById('username').value,document.getElementById('password').value)" />
  63. </td>
  64. <td>
  65. <input type="reset" value="清空" />
  66. </td>
  67. <td><span id="msg"></span></td>
  68. </tr>
  69. </table>
  70. </body>
  71. </html>

DAL.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. namespace AJAXtest
  8. {
  9. public class DAL
  10. {
  11. private string connstr = "server=acer-pc;database=mydatabase;user id=sa;password=123456";
  12. public DataTable selectDB(string sql)
  13. {
  14. DataTable dt = new DataTable();
  15. try
  16. {
  17. SqlConnection conn = new SqlConnection(connstr);
  18. SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
  19. sda.Fill(dt);
  20. }
  21. catch(Exception e)
  22. {}
  23. return dt;
  24. }
  25. }
  26. }

BLL.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. namespace AJAXtest
  7. {
  8. public class BLL
  9. {
  10. public bool login(string username,string password)
  11. {
  12. try
  13. {
  14. string sql = "select password from Users where username='" + username + "'";
  15. DAL sqlSelect = new DAL();
  16. DataTable dt = sqlSelect.selectDB(sql);
  17. if (dt.Rows[0]["password"].ToString() != password)
  18. return false;
  19. }
  20. catch (Exception)
  21. {
  22. }
  23. return true;
  24. }
  25. }
  26. }

Server.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace AJAXtest
  8. {
  9. public partial class Server : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. string username = Request["username"].ToString();
  14. string password = Request["password"].ToString();
  15. BLL b = new BLL();
  16. if (b.login(username, password))
  17. {
  18. Response.Write("登录成功");
  19. Response.End();
  20. }
  21. else
  22. {
  23. Response.Write("登录失败");
  24. Response.End();
  25. }
  26. }
  27. }
  28. }

 Server.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Server.aspx.cs" Inherits="AJAXtest.Server" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. </div>
  12. </form>
  13. </body>
  14. </html>

 

以上所述就是本文的全部内容了,希望大家能够喜欢。

人气教程排行