这篇文章主要介绍了Ajax+asp.net实现用户登陆,主要是为了练习ajax的使用方法,有需要的小伙伴参考下。

以用户登录为例练习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 bj = 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="登录" nclick="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实现用户登陆 转自http://www.shangxueba.com/jingyan/2933319.html的更多相关文章

  1. asp.net Cookie 用户登陆时记住我

    /// <summary> /// 判断Cookie中存储的数据 /// </summary> protected void CheckUserCookie() { //先判断 ...

  2. ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要

    ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm --  Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...

  3. django搭建一个小型的服务器运维网站-用户登陆与session

    目录 项目介绍和源码: 拿来即用的bootstrap模板: 服务器SSH服务配置与python中paramiko的使用: 用户登陆与session; 最简单的实践之修改服务器时间: 查看和修改服务器配 ...

  4. IOS开发之记录用户登陆状态

    上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登 ...

  5. 使用Struts框架,实现用户登陆功能

    前言:本篇文章是本人这周学习的一个小结,在自我总结的同时,希望也能够给其他同学带来一点帮助.本文主要知识是参照书本上的知识点以及网上其他博客文章,在上机操练后的所得,具体源码主要来自http://bl ...

  6. Linux常用命令学习4---(挂载命令mount umount、用户登陆查看和用户交互命令 w who last lastlog)

    紧接着上一篇Linux的命令行的学习:Linux学习3---(文件的压缩和解压缩命令zip unzip tar.关机和重启命令shutdown reboot……) 1.挂载命令     简介      ...

  7. [PHP] - Laravel - 用户登陆中间件

    前言 Laravel 4中,可以使用Route::filter,而在Laravel 5中,没有了filter.php文件,官方建议使用中间件做. 下面是用户登陆的测试例子,涉及到的一些方法和使用,先参 ...

  8. [转]mvc3 使用session来存储类来存储用户登陆信息

    mvc3 使用session来存储类来存储用户登陆信息 2013-08-26 09:48:56|  分类: NET开发 |举报 |字号 订阅   项目之前的登陆机制是这样的:用户登陆后初始化一个类,类 ...

  9. PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: POC http://localhost/p ...

随机推荐

  1. Java学习之InputStream中read()与read(byte[] b)

    Java学习之InputStream中read()与read(byte[] b) 这两个方法在抽象类InputStream中都是作为抽象方法存在的, JDK API中是这样描述两者的: read() ...

  2. tomcat oracle 连接池配置

    <?xml version='1.0' encoding='utf-8'?> <Context displayName="zcgl" docBase=" ...

  3. 面向对象程序设计-C++_课时13初始化列表

    构造函数设置成员初值方法有两种:一种是在函数体内赋值,另一种是采用初始化列表的形式. 初始化列表BETTER 函数体内赋值 类名::类名(形参1,形参2,...形参n) { 数据成员1=形参1; 数据 ...

  4. 性能优化工具---sar

    简介: System Activity Reporter系统活动情况报告 下载地址: http://pagesperso-orange.fr/sebastien.godard/download.htm ...

  5. ubuntu 64位设置兼容32位 for ADB 命令无法运行

    在虚拟机上Ubuntu系统里安装ADT开发工具,配置好环境后导入Android工程报错: 找不到Adb命令: ubuntu 12.04 64位设置兼容32位的实现REF:http://www.2cto ...

  6. SQL Convert XML to Table

    将xml nodes 属性中的值 转为table 形式 declare @xml2 xml set @xml2 = '<CMADatas> <CMAData CmaName=&quo ...

  7. JAVA编译中拒绝访问的问题及解决方案

    在java编译时出现,可以将C盘内的文件转移到其他盘,此问题可能是权限不足不能够读取C盘文件造成的. 文件名与类名要一致,包括大小写,也是要一致!

  8. 选项卡 js操作

    html代码展示(这里展示的是关于日程的标签页)css样式这里省略了>>>>自己写的可能更好看 <div class="row"> <ul ...

  9. 使用反射类、Class类获取指定的构造器并实例化对象

    package com.test; public class Car { private String brand; private String color; private int maxSpee ...

  10. 表单元素的submit()方法和onsubmit事件(转)

    1.表单元素中出现了name="submit"的元素 2.elemForm.submit();不会触发表单的onsubmit事件 3.动态创建表单时遇到的问题 表单元素拥有subm ...