[原]Ajax簡單客戶登陸驗證

來源:互聯網
上載者:User

   
伺服器端操作方便之處我就不吹了,地球人都知道,它最煩莫過於頁面重新整理,頭都被刷暈了,而且他在重新整理的時候,還觸發伺服器端的事件(解決方案: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;
  }
    
   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.