效果图:

                 数据库里面没有这个用户时                                                      数据库里面有这个用户时
 
数据库文件:



JS代码:

// 声明XmlHttpRequest

var xmlHttp;

function CheckName(userName)
{
    createXMLHTTP(); 
//创建XmlHttpRequest对象
    var url="DisposeEvent.aspx?Name="+userName;
    xmlHttp.open(
"GET",url,true);
    xmlHttp.onreadystatechange
=checkUserName;
    xmlHttp.send(
null);
}


//执行检测用户回调函数
function checkUserName()
{
    
if(xmlHttp.readyState==4//判断对象状态
    {
        
if(xmlHttp.Status==200//信息成功返回,开始处理信息
        {
            
if(xmlHttp.responseText=="false")
            
{
               document.getElementById(
'cell1').bgColor="green"
               document.getElementById(
'Btnlogin').disabled="";
            }

            
else
            
{
               document.getElementById(
'cell1').bgColor="red";
               document.getElementById(
'Btnlogin').disabled="false";
            }

        }

    }

}


function createXMLHTTP()
{
    
if(window.XmlHttpRequest)
    
{
        xmlHttp
=new XMLHttpRequest(); //mozilla游览器
    }

    
else if(window.ActiveXObject)
    
{
        
try
        
{
            xmlHttp
=new ActiveXObject("Msxml2.XMLHTTP"); //IE老版本
        }

        
catch(e)
        
{}
        
try
        
{
            xmlHttp
=new ActiveXObject("Microsoft.XMLHTTP"); //IE新版本
        }

        
catch(e)
        
{}
        
if(!xmlHttp)
        
{
            window.alert(
"不能创建创建XmlHttpRequest对象!");
            
return false;
        }

    }

}


静态页面Html代码:

<!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>
    
<title>AJAX登录</title>
<script type="text/javascript" language="javascript" src="JScript.js">

</script>
</head>
<body style="text-align: center">
    
<table style="width: 456px">
        
<tr>
            
<td style="width: 100px">
                用户名:
</td>
            
<td align="left" colspan="2" id="cell1">
                
<input id="txtUserName" type="text" onkeyup="CheckName(document.getElementById('txtUserName').value);" maxlength="2000" /></td>
        
</tr>
        
<tr>
            
<td style="width: 100px">
                密码:
</td>
            
<td align="left" colspan="2">
                
<input id="txtPwd" type="text" /></td>
        
</tr>
        
<tr>
            
<td colspan="3" style="height: 26px">
                
<input id="Btnlogin" type="button" value="注册" /></td>
        
</tr>
    
</table>

</body>
</html>


AJAX回传处理页面代码(DisposeEvent.aspx):

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.SqlClient;

public partial class DisposeEvent : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            Jude();
        }

    }


    
private void Jude()
    
{
        
string UserName = Request.QueryString["Name"].ToString();
        
string con = ConfigurationManager.ConnectionStrings["WebShopConnectionString"].ConnectionString;
        SqlConnection dbConnection 
= new SqlConnection(con);
        dbConnection.Open();
        SqlCommand cmd 
= new SqlCommand("select count(*) from Users where userName='" + UserName + "'", dbConnection);
        
int count = Convert.ToInt32(cmd.ExecuteScalar());
        
if (count > 0)
        
{
            Response.Write(
"true");
            Response.End();
        }

        
else
        
{
            Response.Write(
"false");
            Response.End();
        }

    }

}




posted on 2007-05-18 19:25  小角色  阅读(277)  评论(0)    收藏  举报