这篇文章主要介绍了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. UTF-8、UTF-16、UTF-32编码的相互转换

    最近在考虑写一个可以跨平台的通用字符串类,首先需要搞定的就是编码转换问题. vs默认保存代码文件,使用的是本地code(中文即GBK,日文即Shift-JIS),也可以使用带BOM的UTF-8.gcc ...

  2. 转:CSS布局奇淫技巧之-宽度自适应

    css这个东西,说难不难,说容易也不容易.我觉得最重要的还是经验的积累,正所谓的不积硅步,无以至千里.这一系列文章讲述几种css特殊布局的实现,也当作为自己做个备忘吧. 首先讲的是三列布局,左右两列宽 ...

  3. poj2350

    #include <stdio.h> #include <stdlib.h> int main() { ],tim,i; scanf("%d",&n ...

  4. ubuntu中如何关闭防火墙?

    只需要输入 root@stgman-desktop:~#  sudo ufw disable 防火墙在系统启动时自动禁用

  5. 获取和设置iframe中的元素

    http://www.cnblogs.com/gao-qiang/archive/2012/09/19/2694336.html http://java-my-life.iteye.com/blog/ ...

  6. Deep Learning(深度学习)学习笔记整理系列之(四)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  7. Html5 Video的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 使用分析函数实现Oracle 10G提供的CONNECT_BY_ISLEAF和CONNECT_BY_ROOT的功能(转载)

    文章转载至:http://blog.csdn.net/wzy0623/article/details/1644049 如果,有侵犯您权益的地方,烦请及时的告知我,我会即刻停止侵权行为 Oracle 1 ...

  9. 交换机access和trunk的一些小结(转)

     以太网端口有 3种链路类型:access.trunk.hybird Access类型端口只能属于1个VLAN 般用于连接计算机 端口: Trunk类型端口可以允许多个VLAN通过,可以接收和发送多个 ...

  10. USACO Section 4.3 Buy low,Buy lower(LIS)

    第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...