基于Ajax技术实现无刷新用户登录功能

代码如下:

// JScript 文件
function usersLogon()
{
  var userName = document.getElementById("txtuserName").value;
  var password = document.getElementById("txtpassword").value;
  var checkCode = document.getElementById("txtCheckCode").value;

  var response = userControl_logon.CheckCodeIsRight(checkCode).value;

  if(userName == "")
  {
    document.getElementById("txtuserName").focus();
    return false;
  }
  else if(password == "")
  {
    document.getElementById("txtpassword").focus();
    return false;
  }
  else if(checkCode =="")
  {
    document.getElementById("txtCheckCode").focus();
    return false;
  }
  else
  {
    if(response == true)
    {
      //判断用户是否存在
      userControl_logon.userNameAndPasswordIsExist(userName,password,userNameIsRight);
    }
    else
    {
      alert("验证码出错");
      userControl_logon.checkCodeOperaotr(refreshCheckCode);
      document.getElementById("txtpassword").value = "";
    }
  }
}
function userNameIsRight(res)
{
  var userName = document.getElementById("txtuserName").value;
  if(res.value == true)
  {
    //用户存在,但要看此用户有没有进入管理留言版权限,
    userControl_logon.userNameIsRight(userName,CallBack);
  }
  else
  {
    alert("用户名或密码错误");
    document.getElementById("txtpassword").value = "";
    OnLoad_checkCode();
  }
}
function CallBack(res)
{
  if(res.value == true)
  {
    hideLogon();
    var url = userControl_logon.returnUrl();
    if ( url.value == 404)
    {
      showDefault();
    }
    else
    {
      document.getElementById("Url").innerHTML = '<a href="' + url.value + '">' + url.value + '</a>'
    }
  }
  else
  {
    alert("对不起你的权限不够");
    document.getElementById("txtpassword").value = "";
    OnLoad_checkCode();
  }
}
//隐藏登录框
function hideLogon()
{
  var element = document.getElementById("hideLogon")
  element.style.display = "none"

}
//显示返回首页
function showDefault()
{
  var element = document.getElementById("Returndefault")
  element.style.display = "block"
}
function OnLoad_checkCode()
{
  userControl_logon.checkCodeOperaotr(refreshCheckCode);
  document.getElementById("txtuserName").focus();
  //  return false;
}
///重新得到新的验证吗
function refreshCheckCode(res)
{
  document.getElementById("txtCheckCode").value = "";
  document.getElementById("lblNumber").innerHTML = res.value;
}
function abce()
{
  alert(document.getElementById("lblNumber").value)
}

下面代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using LHB_SQL_2005;

public partial class userControl_logon : System.Web.UI.UserControl
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!this.IsPostBack)
    {
      AjaxPro.Utility.RegisterTypeForAjax(typeof(userControl_logon));
    }
  }

  [AjaxPro.AjaxMethod]
  public static string checkCodeOperaotr()
  {
    string _checkCode = GeneralMethod.GenerateCheckCode();
    System.Web.HttpContext.Current.Session["checkCode"] = _checkCode;
    //返回验证码
    return _checkCode;
  }

  /// <summary>
  /// 判断验证是否正确
  /// </summary>
  /// <param name="checkCode"></param>
  /// <returns></returns>
  [AjaxPro.AjaxMethod]
  public static bool CheckCodeIsRight(string checkCode)
  {
    string _checkCode = (string)(System.Web.HttpContext.Current.Session["checkCode"]);
    if (_checkCode == checkCode)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  /// <summary>
  /// 判断用户名及密码添加是否正确
  /// </summary>
  /// <param name="userName">用户名</param>
  /// <param name="_password">用户名密码</param>
  /// <returns>bool</returns>
  [AjaxPro.AjaxMethod]
  public static bool userNameAndPasswordIsExist(string userName, string _password)
  {
    string password = GeneralMethod.ToEncryptPassword(_password);
    string executeString = "SELECT COUNT(*) FROM users WHERE userName = '" + userName.ToString() + "' AND password = '" + password + "'";
    int count = int.Parse(GetCommand.ExecuteScalar(executeString));
    if (count == 1)
    {
      System.Web.HttpContext.Current.Session["userName"] = userName;
      return true;
    }
    else
    {
      return false;
    }
  }

  /// <summary>
  /// 判断用户是不是有这进入管理留言版的权限
  /// </summary>
  /// <param name="userName">用户名</param>
  /// <returns></returns>
  [AjaxPro.AjaxMethod]
  public static bool userNameIsRight(string userName)
  {
    string executeString = "SELECT [right] FROM role WHERE usersId = (select userNameId from users where userName = '" + userName + "')";
    int count = int.Parse(GetCommand.ExecuteScalar(executeString));
    if (count > 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  /// <summary>
  /// 返回Url值
  /// </summary>
  /// <returns></returns>
  [AjaxPro.AjaxMethod]
  public static string returnUrl()
  {
    string url = "";
    try
    {
      url = System.Web.HttpContext.Current.Session["url"].ToString();
    }
    catch
    {
      url ="404";
    }
    return url;
  }
}

下面是页面代码

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="logon.ascx.cs" Inherits="userControl_logon" %>
<script language="javascript" type="text/javascript" src="../JavaScript/logon.js">
</script>
<script language="javascript" type="text/javascript" src="JavaScript/logon.js">
</script>
<link href="../CSS/table_css.css" rel="stylesheet" type="text/css" />
<link href="CSS/table_css.css" rel="stylesheet" type="text/css" />
<body onload="OnLoad_checkCode();">
<div>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td>
        <table id="hideLogon" border="0" cellpadding="0" cellspacing="0" style="display:block;">
          <tr>
            <td style="background-color: #99ccff">用户名:</td>
            <td><input type="text" id="txtuserName" style="width: 105px" /></td>
          </tr>
          <tr>
            <td style="background-color: #99ccff">密 码:</td>
            <td>
              <input id="txtpassword" type="password" style="width: 105px" /></td>
          </tr>
          <tr>
            <td style="background-color: #99ccff">验证码:</td>
            <td style="background-color: #99ccff">
              <input type= "text" id="txtCheckCode" style=" width:60px" /><label id="lblNumber"></label></td>
          </tr>
          <tr>
            <td style="background-color: #99ccff"></td>
            <td style="background-color: #99ccff">
              <input type="button" onclick="usersLogon();" value="登录" id="btnLogon" /></td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td >
        <div id="Url"></div>
      </td>
    </tr>
    <tr>
      <td align="center">
        <table id="Returndefault" border="0" cellpadding="0" cellspacing="0" style="display:none;">
          <tr>
            <td>
              <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">返回首页</asp:HyperLink></td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</div>
</body>

以上所述是小编给大家介绍的基于Ajax技术实现无刷新用户登录功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • 菜鸟蔡之Ajax复习第三篇(Ajax之无刷新登录)

    好了,还是和以前一样,先建一个Login.html页面吧,用于填写登录信息,然后再建一个DealData.aspx页面吧(当然,这里用一般处理程序是最好的了:*.ashx),用来处理数据用的. 在login.html页面中其代码如下: 复制代码 代码如下: <head> <title>无刷新登录</title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript&qu

  • 基于jquery ajax 用户无刷新登录方法详解

    Ajax框架就是提供模块化实现Ajax功能的集合,Ajax框架可以是各种语言实现的(比如SAJAX有各种语言的实现),Ajax只是jquery中的一部分, 实例1 复制代码 代码如下: $.ajax({ type:'post',//可选get url:'action.php',//这里是接收数据的PHP程序 data:'data='dsa',//传给PHP的数据,多个参数用&连接 dataType:'text',//服务器返回的数据类型 可选XML ,Json jsonp script html

  • asp.net+jquery ajax无刷新登录的实现方法

    因为工作需要研究了一下 js的ajax,下面是成果.主要是三部分:一.js部分 友情提示注意js部分中的 $("#btn_login")中的return false:这个可以阻止回转服务器不然还是会刷新 复制代码 代码如下: $(document).ready(function () {    $("#btn_login").click(function () {        postlogin();        return false;    });});

  • 基于Ajax技术实现无刷新用户登录功能

    代码如下: // JScript 文件 function usersLogon() { var userName = document.getElementById("txtuserName").value; var password = document.getElementById("txtpassword").value; var checkCode = document.getElementById("txtCheckCode").val

  • tp5框架基于Ajax实现列表无刷新排序功能示例

    本文实例讲述了tp5框架基于Ajax实现列表无刷新排序功能.分享给大家供大家参考,具体如下: 在后台管理的时候我们有时需要对数据进行排序,以控制数据在模板显示的顺序,排序的原理就是修改数据库,然后更新视图.我们可以单独写一个方法来实现排序的功能,成功后刷新页面,也可以利用Ajax技术,实现数据的局部请求,也就是无刷新排序的功能. 现在想要达到的效果是在排序的input框中输入数值,点击排序实现无刷新排序的功能. 首先是表格(cate.html)这一块我们要单独摘出来,放入到一个单独页面当中,方便

  • Jquery基于Ajax方法自定义无刷新提交表单Form实例

    本文实例讲述了Jquery基于Ajax方法自定义无刷新提交表单Form的方法.分享给大家供大家参考.具体实现方法如下: Jquery的$.ajax方法可以实现ajax调用,要设置url,post,参数等. 如果要提交现有Form需要写很多代码,何不直接将Form的提交直接转移到ajax中呢. 以前的处理方法: 如Form代码如下: 复制代码 代码如下: <form id="Form1" action="action.aspx" method="pos

  • Ajax技术(WEB无刷新提交数据)-

    Ajax内部交流文档一.使用Ajax的主要原因 1.通过适当的Ajax应用达到更好的用户体验: 2.把以前的一些服务器负担的工作转嫁到客户端,利于客户端闲置的处理能力来处理,减轻服务器和带宽的负担,从而达到节约ISP的空间及带宽租用成本的目的. 二.引用 Ajax这个概念的最早提出者Jesse James Garrett认为: Ajax是Asynchronous JavaScript and XML的缩写. Ajax并不是一门新的语言或技术,它实际上是几项技术按一定的方式组合在一在同共的协作中发

  • AJAX如何实现无刷新登录功能

    最近学习了如何实现无刷新登录,大体的效果如下(界面比较丑,请自行忽略....): 点击登录按钮时弹出登录窗口,输入正确的用户名密码后点击登录则登录窗口关闭,状态改为当前用户名. 第一步: 首先弹出窗口使用的是jquery-ui中的控件,第一步要学会如何使用. 打开解压后的jquery-UI下的development-bundle->demos,找到index.html,选择dialog下的model dialog,右键查看源码,观察如何使用该控件,找到一句关键代码:$("#dialog-m

  • Ajax+smarty技术实现无刷新分页

    这里运用Smarty模板,更简单 本文主要的技术:AJAX,PHP,Smarty,另外自己封装了一个很简单的类 类: (function(){ function $(id) { return document.getElementById(id); } $.init=function() { try{return new XMLHttpRequest();}catch(e){}; try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e

  • 基于Ajax技术实现考试倒计时并自动提交试卷

    1.概述 在开发网络考试系统时,考试计时并自动提交试卷是必不可少的功能.由于在答卷过程中,试卷不能刷新,所以需要使用Ajax实现无刷新操作.运行本实例,访问准备考试页面index.jsp,在该页面中,单击"开始考试"按钮,将打开新窗口显示开始考试的页面,如图10.1所示,页面会自动计时,当考试时间结束时,将自动提价试卷. 2.技术要点 主要是利用Ajax异步提交技术和Servlet技术实现的.显示在考试页面中的计时时间是在Servlet中设置的,需要通过Ajax的异步提交不断的请求Se

  • ThinkPHP5.1+Ajax实现的无刷新分页功能示例

    本文实例讲述了ThinkPHP5.1+Ajax实现的无刷新分页功能.分享给大家供大家参考,具体如下: 无刷新分页可以减轻服务器负担,利用Ajax技术,请求部分信息,提高网站访问速度,是网站建设的必备技术. 需要在后台展示自定义属性列表(lst.html),其中的列表部分摘出来,放到(paginate1.html)中: <div class="row"> <div class="col-sm-12"> <div class="i

  • 基于jQuery实现的无刷新表格分页实例

    本文实例讲述了基于jQuery实现的无刷新表格分页.分享给大家供大家参考,具体如下: 效果图如下: html结构: <table id="cs_table" class="datatable"></table> css样式: html,body{margin: 0;padding:0} a:focus {outline: none;} /* 通用表格显示 */ table, th, td {font: 12px Arial,Helvetica

  • jquery+ajax+C#实现无刷新操作数据库数据的简单实例

    我们知道同步执行和异步执行的区别,为了更好的提高用户的体验,我们都会采用异步方式去处理一些问题,毕竟单线程的同步可能回造成卡死等现象,很不友好,所以可以使用ajax来完成用户的体验,现在我们就来说说如何使用jquery的ajax来实现无刷新的获取内容 我们只是单方面的获取内容,分页等就不考虑了,后期会讲到无刷新的分页 页面中我们放置一个Div容器用来存放返回的内容 <div id="comment"><img src="images/Wait.gif&quo

随机推荐