AJAX实例:实现登陆验证

  ajax实现登陆验证    --------------------------------------------------------------------------------
    js文件如下:
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    var xmlHttp;
    function IMG1_onclick() {
    CheckIsValid();
    }
    function CheckIsValid()
    {
    CreatXmlHttpRequest();
    var userName=document.getElementById("AdminText");
    var passWord=document.getElementById("AdminPasswordText");
    var url="CheckLogin.aspx?userName="+escape(userName.value)+"&passWord="+escape(passWord.value);
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=callback;
    xmlHttp.send(null);
    }
    function CreatXmlHttpRequest()
    {
    try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
    xmlHttp = false;
    }
    }
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
    }
    }
    function callback()
    {
    if(xmlHttp.readyState==4)
    {
    if(xmlHttp.status==200)
    {
    var mes=xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
    var val=xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;
    setMessage(mes,val);
    }
    }
    }
    function setMessage(message,isValid)
    {
    var messageArea=document.getElementById("showMessage");
    var fontColor;
    if(isValid =="True")
    {
    window.location.href="ManagerControl.aspx";
    }
    else
    {
    fontColor="red";
    imgUrl="images/404.jpg";
    messageArea.innerHTML="<img src="+imgUrl+"/>"+"<font color="+fontColor+">"+message+"</font>";
    }
    }
    cs文件如下:
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    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.Data.Sql;
    using System.Data.SqlClient;
    public partial class CheckLogin : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Response .ContentType="text/xml";
    bool passed = ValiLogin(Request["userName"], Request["passWord"]);
    string message = "出错啦,密码错误或者管理员名称错误";
    if (passed)
    {
    message = "登陆成功";
    }
    string textxml;
    textxml = "<response>"+"<passed>"+passed.ToString()+"</passed>"+"<message>"+message+"< /message>"+"</response>";
    Response.Write(textxml);
    }
    private bool ValiLogin(string userName,string passWord)
    {
    bool isPassed = true;
    if ((userName == ConfigurationManager.AppSettings["AdminName"]) && (passWord == ConfigurationManager.AppSettings["PassWord"]))
    {
    isPassed = true;
    }
    else
    {
    isPassed = false;
    }
    return isPassed;
    }
    }
    HTML代码如下:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Manager.aspx.cs" Inherits="Manager" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <meta http-equiv="content-type" content="text/html; charset=GB2312" />
    <title>王子的宫殿 http://www.dwww.cn </title>
    <meta name="keywords" content=http://dwww.cn />
    <meta name="description" content="dwww.cn" />
    <link rel="stylesheet" type="text/css" href="default.css" />
    <script language="javascript" type="text/javascript" src="js/main.js">
    </script>
    </head>
    <body>
    <div id="outer">
    <div id="upbg" style="left: 0px; top: 0px"></div>
    <div id="inner">
    <div id="header">
    <h1><span>王子</span>宫殿<sup>1.0</sup></h1>
    <h2>by 薛晓龙</h2>
    </div>
    <div id="splash"></div>
    <div id="menu">
    <ul>
    <li class="first"><a href="Default.aspx">主页</a></li>
    <li><a href="#">博客</a></li>
    <li><a href="MyPhotos.html">相册</a></li>
    <li><a href="#">留言</a></li>
    <li><a href="Myinfo.aspx">联系我</a></li>
    <li><a href="Manager.aspx">后台管理</a></li>
    </ul>
    <div id="date">August 1, 2006</div>
    </div>
    <div id="primarycontent" style="left: 200px; top: 0px; width: 204px; margin-top:100px">
    <div id="hreader"><img src="images/logins.gif" alt="登陆" /></div>
    <div id="Login">
    <label for="userName">用户名:</label><input id="AdminText" type="text" />&nbsp;<br />
    <label for="passWord">
    &nbsp; &nbsp; 密码:</label><input id="AdminPasswordText" type="password" />
    <br />
    <br />
    <img id="loginIMG" src="images/login.gif" alt="登陆" style="float:right; cursor:pointer" onclick="return IMG1_onclick()"/>
    </div>
    </div>
    <div id="shopPlace" style="width:100%; height:200px;">
    <span id="showMessage" style="text-align:center; margin-left:200px;"></span>
    </div>
    </div>
    </div>
    </body>
    </html>