怎么在textbox7插入数据库前判断已存在并弹出提示框,就像下面已经写好的”用户名已存在那样“
namespace InternetShopping.AdminManage

    public partial class UserRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string num = ("Server=(local);Integrated Security=SSPI;database=bookshopping");
            SqlConnection connect = new SqlConnection(num);
            string sql = "select count(*) from Custom";
            SqlCommand command = new SqlCommand(sql, connect);
            connect.Open();
            int numcount = Convert.ToInt32(command.ExecuteScalar());
            numcount++;
         TextBox7.Text=numcount.ToString ();
        
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            string UserName = this.TextBox1.Text;
            TextBox1.Text = "";
            string num = ("Server=(local);Integrated Security=SSPI;database=bookshopping");
            SqlConnection connect = new SqlConnection(num);
            string sql = "select *from Custom where customName='" + UserName + "'";
            SqlCommand command = new SqlCommand(sql, connect);
            
            connect.Open();
            int num1 = Convert.ToInt32(command.ExecuteScalar());
            if (num1 > 0 )
            {
                Response.Write("<script>alert('用户名已存在');</script>");
            }
            else
            {
                SqlDataSource1.Insert();
                Response.Redirect("BuyComputerBooks.aspx");
            }
            connect.Close();}}
}

解决方案 »

  1.   

    ASHX + AJAX 吧
      

  2.   

    namespace InternetShopping.AdminManage

        public partial class UserRegister : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               string txtName=TextBox7.Text;
                string num = ("Server=(local);Integrated Security=SSPI;database=bookshopping");
                SqlConnection connect = new SqlConnection(num);
                string sql = "select txtName from Custom where txtName="txtName";
                SqlCommand command = new SqlCommand(sql, connect);
                connect.Open();
                int numcount = Convert.ToInt32(command.ExecuteScalar());
               if (numcount.length>0 )
                {
                    Response.Write("<script>alert('用户名已存在');</script>");
                }
            
            }
           
      

  3.   

    做一个光标离开事件,然后在事件中查询数据库,这是简单的,无刷新就得用ajax
      

  4.   

    控件有个属性,mouseleave,应该是这样拼,光标重控件中离开触发的事件,你把验证写在这个里面
      

  5.   


    protected void Button1_Click1(object sender, EventArgs e)
    {
                string UserName = this.TextBox1.Text;
                TextBox1.Text = "";
                string num = ("Server=(local);Integrated Security=SSPI;database=bookshopping");
                SqlConnection connect = new SqlConnection(num);
                string sql = "select *from Custom where customName='" + UserName + "'";
                SqlCommand command = new SqlCommand(sql, connect);
                
                connect.Open();
                int num1 = Convert.ToInt32(command.ExecuteScalar());
                if (num1 > 0 )
                {
                    Response.Write("<script>(function(){alert('用户名已存在');})()</script>");
                }
                else
                {
                    SqlDataSource1.Insert();
                    Response.Redirect("BuyComputerBooks.aspx");
                }
                connect.Close();}
      

  6.   

    这个事件坑爹的。。如果你的光标就没有上去过  这个事件是不会触发的  最妥的办法就是 Ajax  或者直接后台保存之前校验。