欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Ajax+asp.net实现用户登陆

程序员文章站 2024-02-14 17:44:16
以用户登录为例练习ajax的使用方法 login.html 

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

login.html

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <title></title>
  <script type="text/javascript">
    var obj = createobj();
 
    function login(name, pwd)
    {
      var urlstr = "http://localhost:14248/server.aspx?username=" + name + "&password=" + pwd;
      obj.open("get", urlstr, true);
      obj.onreadystatechange = dowork;
      obj.send();
    }
 
    function dowork()
    {
      if (obj.readystate == 4)
      {
        if (obj.status == 200)
        {
          document.getelementbyid("msg").innertext = obj.responsetext;
        }
      }
    }
    //创建对象
    function createobj()
    {
      var xmlhttp = null;
      try {
        //非ie浏览器
        xmlhttp = new xmlhttprequest();
      }
      catch (e)
      {  //ie浏览器
        try{
          xmlhttp = new activexobject("msxml2.http");
        }
        catch (e)
        {
          xmlhttp = new activexobject("microsoft.xmlhttp");
        }
      }
      return xmlhttp;
    }
  </script>
</head>
<body>
  <table>
    <tr>
      <td align="center" colspan="2">登录</td>
    </tr>
    <tr>
      <td>用户名:</td>
      <td><input type="text" id="username" name="username" /></td>
    </tr>
    <tr>
      <td>密码:</td>
      <td><input type="password" id="password" name="password" /></td>
    </tr>
    <tr>
      <td >
        <input type="submit" value="登录" onclick="login(document.getelementbyid('username').value,document.getelementbyid('password').value)" />
      </td>
      <td>
        <input type="reset" value="清空" />
      </td>
      <td><span id="msg"></span></td>
    </tr>
  </table>
</body>
</html>

dal.cs

using system;
using system.collections.generic;
using system.data;
using system.data.sqlclient;
using system.linq;
using system.web;
 
namespace ajaxtest
{
  public class dal
  {
    private string connstr = "server=acer-pc;database=mydatabase;user id=sa;password=123456";
    public datatable selectdb(string sql)
    {
      datatable dt = new datatable();
      try
      {
        sqlconnection conn = new sqlconnection(connstr);
        sqldataadapter sda = new sqldataadapter(sql, conn);
        sda.fill(dt);
      }
      catch(exception e)
      {}
      return dt;
    }
  }
}

bll.cs

using system;
using system.collections.generic;
using system.data;
using system.linq;
using system.web;
 
namespace ajaxtest
{
  public class bll
  {
    public bool login(string username,string password)
    {
      try
      {
        string sql = "select password from users where username='" + username + "'";
        dal sqlselect = new dal();
        datatable dt = sqlselect.selectdb(sql);
        if (dt.rows[0]["password"].tostring() != password)
          return false;
      }
      catch (exception)
      { 
      }
      return true;
    }
  }
}

server.aspx.cs

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
 
namespace ajaxtest
{
  public partial class server : system.web.ui.page
  {
    protected void page_load(object sender, eventargs e)
    {
      string username = request["username"].tostring();
      string password = request["password"].tostring();
      bll b = new bll();
      if (b.login(username, password))
      {
        response.write("登录成功");
        response.end();
      }
      else
      {
        response.write("登录失败");
        response.end();
      }
    }
  }
}

 server.aspx

<%@ page language="c#" autoeventwireup="true" codebehind="server.aspx.cs" inherits="ajaxtest.server" %>
 
<!doctype html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
   
  </div>
  </form>
</body>
</html>

Ajax+asp.net实现用户登陆 

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