none
无刷新验证用户名----大菜鸟求助啊,真的很无助!!! RRS feed

  • 问题

  • 我这代码当焦点离开textbox时不仅刷页面,而且Label 的内容也一直不变,请大家帮忙
    //页面
     <script type="text/javascript">
       
            var req;
            function validate()
            {
                var username=document.getElementById("Uname_TextBox").value;
                if ((username == null) || (username == ""))
                    return;
                var url="Validate.aspx?userid=" + escape(username);
                if(window.XMLHttpRequest)
                {
                    req=new XMLHttpRequest();
                }
                else if(window.ActiveXobject)
                {
                    req=new ActiveXObject("microsoft.XMLHTTP");
                }
                req.open("Get",url,true);
                req.onreadystatechange=callback;
                req.send(null);
            }
            function callback()
            {
            if(req.readystate<4)
                document.getElementById("Message").innerText="验证用户...";
                if(req.readystate==4)
                    if(req.status==200)
                    {
                        document.getElementById("Message").innerText=req.responseText;                   
                    }
            }
        </script>

    <asp:TextBox ID="Uname_TextBox" Width="128px" Height="12px" runat="server" onblur="javascript:validate();" AutoPostBack="true"></asp:TextBox>
    <asp:Label ID="Message" runat="server" Text="请输入用户名"></asp:Label>

    //Validate .aspx
        public partial class Validate : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string userid = Request.QueryString["userid"].ToString();
                UserInfoBLL userinfo = new UserInfoBLL();
                if (userinfo.CheckUserIDExist(userid))
                {
                    Response.Write("用户名存在");
                }
                else
                {
                    Response.Write("用户名可用");
                }
            }
        }

    //BLL
            //验证UserID是否存在
            public bool CheckUserIDExist(string userid)
            {
                return (dal.CheckUserIDExist(userid));
            }

    //DAL
     //验证USerID是否存在
            public bool CheckUserIDExist(string userid)
            {
                select = "select count(*) from UserInfo where UserID=" + userid;
                comm = new SqlCommand(select, conn);
                SqlDataReader reader = comm.ExecuteReader();
                //存在返回true
                if (reader.HasRows)
                {
                    conn.Close();
                    return (true);
                }
                //不存在返回false
                else
                {
                    conn.Close();
                    return (false);
                }
            }
    • 已移动 桂素伟Moderator 2009年12月18日 15:04 (发件人:微软 .NET 俱乐部(INETA))
    2009年10月2日 5:55

答案