[原]Ajax简单客户登陆验证
[作者]:菩提树下的杨过 [来源]:互联网 [收录时间]:2007-7-30 21:50:54

  服务器端操作方便之处我就不吹了,地球人都知道,它最烦莫过于页面刷新,头都被刷晕了,而且他在刷新的时候,还触发服务器端的事件(解决方案: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
  


3、在 与间加入一些引用如下:



二、介绍正题-用户登录验证
1、前台HTML:















usename
pwd

2、引用Js文件


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;
}