//声明XMLHttpRequest对象
var xmlHttp;//检测用户名是否存在
function CheckName(userName)
{
    createXMLHTTP();//创建XMLHttpRequest对象
    var url="DisposeEvent.aspx?userName="+userName+"&Event=Check";
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=checkUserName;
    xmlHttp.send(null);
}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;
        }
    }
}//执行检测用户名回调函数
function checkUserName()
{
    if(xmlHttp.readyState==4)//判断对象状态
    {
        if(xmlHttp.status==200)//信息成功返回,开始处理信息
        {
            if(xmlHttp.responseText=="true")
            {
                document.getElementById("imgName").src="images/true.gif";
                //让注册按钮失效
                document.getElementById("btnReg").disabled=false;
            }
            else
            {
                document.getElementById("imgName").src="images/false.gif"; 
                document.getElementById("btnReg").disabled=true;                         
            }
        }
    }
}

解决方案 »

  1.   


    public partial class DisposeEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = new User();
            //是否为执行CheckName方法
            if (Request.QueryString["Event"].ToString() == "Check")
            {
                if (user.checkName(Request.QueryString["userName"].ToString()))
                {
                    //当数据库中已存在此用户时输出为false,让其显示相应图标
                    Response.Write("false");
                    Response.End();
                }
                else
                {
                    Response.Write("true");
                    Response.End();
                }
            }
        }
    }
      

  2.   


    public class User
    {
        public User()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        //检测用户名是否存在
        string conStr = System.Configuration.ConfigurationSettings.AppSettings["HumanResources"];
        public bool checkName(string userName)
        {
            SqlConnection userConnection = new SqlConnection(conStr);
            SqlCommand userCommand = new SqlCommand("checkName", userConnection);
            userCommand.CommandType = CommandType.StoredProcedure;//采用存储过程
            userCommand.Parameters.Add("@userName", SqlDbType.VarChar, 50);//存储过程参数
            userCommand.Parameters["@userName"].Value = userName;//给参数赋值
            userCommand.Parameters.Add("@count",SqlDbType.Int);
            userCommand.Parameters["@count"].Direction = ParameterDirection.Output;
            userCommand.Connection.Open();//打开连接
            userCommand.ExecuteScalar();//返回首行首列,如果存在的话返回1
            int n =Convert.ToInt32( userCommand.Parameters["@count"].Value);
            userCommand.Connection.Close();//关闭连接
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
      

  3.   

    http://topic.csdn.net/u/20080616/08/7d35368b-f257-4920-8f27-e0ff474e14f5.html
    ==
    这里我不是回过么
      

  4.   

    在你自己的input的中加
    <input id="userName" type="text" onkeyup="CheckName(document.getElementById('userName').value);" />