当前位置: 首页 > 编程笔记 >

Ajax+asp.net实现用户登陆

孔乐邦
2023-03-14
本文向大家介绍Ajax+asp.net实现用户登陆,包括了Ajax+asp.net实现用户登陆的使用技巧和注意事项,需要的朋友参考一下

以用户登录为例练习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>

 

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

 类似资料:
  • 本文向大家介绍php实现用户登陆简单实例,包括了php实现用户登陆简单实例的使用技巧和注意事项,需要的朋友参考一下 php实现用户登陆简单实例 前言: 最近要完成的最后一个部分,就是对用户提交的数据进行管理,至于管理,那肯定就是管理员的事了,那一定涉及登陆,验证账号权限,账号是否过期等等问题。 所需知识 session,确实是很重要的东西。并且我遇到session不能跨页,修改PHP.ini的se

  • 本文向大家介绍struts2+jquery实现ajax登陆实例详解,包括了struts2+jquery实现ajax登陆实例详解的使用技巧和注意事项,需要的朋友参考一下 文本仪一个实例讲述了struts2+jquery实现ajax登陆的实现方法,具体步骤如下: 一、新建一个web项目,取名test。配置好struts2的环境,并导入Jquery的js文件到该项目。 二、在com.action包下,新

  • 本文向大家介绍asp.net登录验证码实现方法,包括了asp.net登录验证码实现方法的使用技巧和注意事项,需要的朋友参考一下 前端添加的标签和方法: 验证码: 然后在项目中在新建一个VerifyCode.aspx,下面是aspx的代码: 接着是aspx.cs的代码: 于是!就可以生成验证码了,然后自己再把编写验证版的判断逻辑写好就可以啦! 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望

  • 本文向大家介绍用AJAX实现页面登陆以及注册用户名验证的简单实例,包括了用AJAX实现页面登陆以及注册用户名验证的简单实例的使用技巧和注意事项,需要的朋友参考一下 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。 AJAX 是一种用于创建快速动态网页的技术。其核心是 JavaScript 对

  • 本文向大家介绍servlet实现用户登录小程序,包括了servlet实现用户登录小程序的使用技巧和注意事项,需要的朋友参考一下 当时在上JAVA课的时候,老师就给我们讲过SUN公司的servlet是一个典型的JAVA语言的应用。现在在上网络编程课的时候,终于接触到了这种服务器小程序。 现在我们就用servlet来简单实现一个用户登录的小程序。 首先,servlet也是一个JAVA类,新建一个JAV

  • 本文向大家介绍application作用域实现用户登录挤掉之前登录用户代码,包括了application作用域实现用户登录挤掉之前登录用户代码的使用技巧和注意事项,需要的朋友参考一下 一、实现思想 1.application(ServletContext)是保存在服务器端的作用域,我们在application中保存两种形式的键值对:1:<userId, sessionId>,2:<sessionI