本人刚学.net,做了个注册表,想实现用户名自动检测存在在数据库中的用户名是否会重复,重复就提示用户名已存在,就像现在很多网站注册页面一样。求高手解答

解决方案 »

  1.   

    在插入数据前,以这个用户名到数据库中查询,如果查询到有的话,就提示已经存在,如果没有就说明该用户名可用。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using USTC;
    using System.Data;public partial class Admin_user_Registration : System.Web.UI.Page
    {
        DM dm = new DM();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownList();
                this.msg.Visible = false;
                this.msg2.Visible = false;
            }
        }    protected void btnRegister_Click(object sender, EventArgs e)
        {
            string name = this.tbUserName.Text.Trim(); //用户名
            string pwd = Common.MD5.ToMD5(this.tbUserPwd.Text.Trim()); //密码
            string realname = this.tbRealName.Text.Trim(); //真实姓名
            string inclass = string.Empty; //所在班级
            if (this.ddlClass.SelectedItem.Text == "--请选择--")
            {
                this.msg2.Visible = true;
                this.msg2.InnerText = "(*)请选择班级!";
            }
            else
            {
                inclass = this.ddlClass.SelectedItem.Text.Trim();
                if (IsUserNameValid(name))
                {
                    //用户名已经存在
                    this.msg.Visible = true;
                    this.msg.InnerText = "(*)该用户名已经存在!";
                }
                else
                {
                    try
                    {
                        //用户名不存在,可以添加
                        string strSQL = "insert into tb_Users(RealName,InClass,Question,Answer) values('" + realname + "','" + inclass + "','" + name + "','" + pwd + "')";
                        dm.execsql(strSQL);                    ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('注册成功!');setTimeout('window.close();',2000);</script>");
                    }
                    catch (Exception)
                    {
     
                    }
                }
            }
        }    /// <summary>
        /// 绑定所在班级
        /// </summary>
        public void BindDropDownList()
        {
            string strSQL = "select * from tb_Class";
            DataSet ds = dm.getsql(strSQL);
            this.ddlClass.DataSource = ds;
            this.ddlClass.DataTextField = "ClassName";
            this.ddlClass.DataValueField = "ClassID";
            this.ddlClass.DataBind();
            this.ddlClass.Items.Insert(0,"--请选择--");
        }    protected void btnReset_Click(object sender, EventArgs e)
        {
            this.tbUserName.Text = "";
            this.tbUserPwd.Text = "";
            this.tbRealName.Text = "";
            this.ddlClass.SelectedIndex = 0;
        }    /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsUserNameValid(string name)
        {
            bool flag = false;
            string strSQL = "select count(Question) as counter from tb_Users where Question='" + name.Trim() + "'";
            DataSet ds = dm.getsql(strSQL);
            if (ds.Tables[0].Rows[0]["counter"].ToString() != "0")
            {
                flag = true;
            }
            return flag;
        }
    }
      

  2.   

    这个就复杂了,设计到ajax请求后台方法,获取返回值后DOM操作,刚学.NET别搞那么花哨的了,把基本的注册,登录,发布留言什么,管理等等搞定了再说吧
      

  3.   

    设置文本框的失去焦点事件,通过AJAX发送到后台,判断是否存在,然后返回信息,赋值给提示Lable。就可以了
      

  4.   

    工作了1年多最近转WEB慢慢开始接触前端的技术 楼主刚学就AJAX JQ- -强悍 你先搞清楚一些原理吧- -
      

  5.   


        <script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
        <script type="text/javascript">
            $(function () {
                $(document).ready(function () {
                    $("#btt").click(function () {
                        $.ajax({
                            url: "Handler1.ashx",
                            type: "post",
                            datatype: "html",
                            data: {userName: $("#TextBox1").val()},
                            success: function (msg) {
                                if (msg != null) {
                                    $("#div1").html("<font color='red'>" + msg + "</font>");
                                }
                            }
                        });
                    });
                });
            });
        </script><body>
        <form id="form1" runat="server">
        <div id="div1">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <input type="button" value="点击我" id="btt" />
        </div>
        </form>
    </body>[code=C#]
            public void ProcessRequest(HttpContext context)
            {
                string userName = context.Request["userName"];
                if (userName != "HelloAccp")
                {
                    string title = "用户名可以用";
                    context.Response.Write(title);
                }
                else
                {
                    context.Response.Write("该用户名已经存在,不可以使用");
                }
            }[/code]