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

asp+Ajax简单客户登陆验证

程序员文章站 2022-07-02 16:57:51
服务器端操作方便之处我就不吹了,地球人都知道,它最烦莫过于页面刷新,头都被刷晕了,而且他在刷新的时候,还触发服务器端的事件(解决方案:http://skylaugh.cnb...
服务器端操作方便之处我就不吹了,地球人都知道,它最烦莫过于页面刷新,头都被刷晕了,而且他在刷新的时候,还触发服务器端的事件(解决方案:http://skylaugh.cnblogs.com/archive/2006/06/05/418010.html),现在ajax的出现,他们的结合是发展的必然!

    一、介绍一下ajax在asp.net中的基本使用
    1、在工程中引入ajax.dll文件。
  ajax.dll实现xmlhttprequest请求服务器的实现细节。.net项目中,添加上对其的引用,就可以进

行相应封装操作了。

    2、在web.config中设置httphandle
    <httphandlers>
      <add verb="post,get" path="ajax/*.ashx" type="ajax.pagehandlerfactory, ajax"/>
    </httphandlers>
    3、在 <head>与</head>间加入一些引用如下:
    <script src=js/xml.js></script> 
    <link href="css/mystyle.css" type="text/css" rel="stylesheet"> 
    <script src="/httpforajax/ajax/common.ashx" type="text/javascript"></script> 
    <script src="/httpforajax/ajax/ttyu.ajaxdata,httpforajax.ashx" type="text/javascript"></script> 

    二、介绍正题-用户登录验证
    1、前台html:
<form id="form1" method="post" runat="server" action="" onsubmit="login.getlogin();return false;">   
    <table id="table1"  cellspacing="1" cellpadding="1" width="300" border="1">
     <tr>
      <td></td>
      <td><input type="text"  id="txtusername">usename</td>
     </tr>
     <tr>
      <td></td>
      <td><input type="password"  id="txtpassword">pwd</td>
     </tr>
     <tr>
      <td></td>
      <td><input type="submit" value="登陆"></td>
     </tr>
    </table>
  </form>

    2、引用js文件

<script language="javascript" src="login.js" type="text/javascript"></script>
  <script language="javascript">
  window.onload = function()
    {
       login=new login(testajax);
      }
  </script>

   login.js文件
// 提取控件值
function getvaluebyid(pobjid){
 var obj=document.getelementbyid(pobjid);
 try{
  return obj.value;
 }catch(e){
  alert("控件:"+pobjid+" 不存在,或没有value属性");
 }
}

function login(obj)
{
 this.obj = obj;
 this.getlogin=function()
 {
  var returnvalue;
  var username=getvaluebyid('txtusername');
  var password=getvaluebyid('txtpassword');
  if(!username||!password)
  {
   alert('请输入用户名与密码!');
   return;
  }  
  try
  {
   returnvalue=this.obj.login(username,password).value;
  }catch(e)
  {
   alert('登录出错,请稍后再试或与管理员联系');
  }
  switch(returnvalue)
  { 

   case 1:
    alert('对不起,您输入的用户名或密码不正确或者不是管理员!');
    break;   
   case 0:
    alert('管理员登录成功!');
    window.document.location.href('../error.aspx');
    break;   
   default:
    alert('登录失败,请稍后再试或与管理员联系'+returnvalue);
    break;
  }
 }
}

3、.cs文件

private void page_load(object sender, system.eventargs e)
  {
   ajax.utility.registertypeforajax(typeof(testajax));
  }

  [ajax.ajaxmethod()]
  public int login(string username,string password)
  {   
   // 管理员登陆入口
   action.common.cdb cdb = new action.common.cdb();
   if("admin"==cdb.exescalar("select upower from users where 

uname='"+username+"' and upwd='"+password+"'"))
    return 0;
   else
    return 1;
  }