asp+Ajax简单客户登陆验证

2019-09-14 07:22:02王冬梅

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