问题描述
怎么在textbox7插入数据库前判断已存在并弹出提示框,就像下面已经写好的”用户名已存在那样“namespaceInternetShopping.AdminManage{publicpartialclassUserRegister:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){stringnum=("Server=(local);IntegratedSecurity=SSPI;database=bookshopping");SqlConnectionconnect=newSqlConnection(num);stringsql="selectcount(*)fromCustom";SqlCommandcommand=newSqlCommand(sql,connect);connect.Open();intnumcount=Convert.ToInt32(command.ExecuteScalar());numcount++;TextBox7.Text=numcount.ToString();}protectedvoidButton1_Click1(objectsender,EventArgse){stringUserName=this.TextBox1.Text;TextBox1.Text="";stringnum=("Server=(local);IntegratedSecurity=SSPI;database=bookshopping");SqlConnectionconnect=newSqlConnection(num);stringsql="select*fromCustomwherecustomName='"+UserName+"'";SqlCommandcommand=newSqlCommand(sql,connect);connect.Open();intnum1=Convert.ToInt32(command.ExecuteScalar());if(num1>0){Response.Write("<script>alert('用户名已存在');</script>");}else{SqlDataSource1.Insert();Response.Redirect("BuyComputerBooks.aspx");}connect.Close();}}}
解决方案
解决方案二:
ASHX+AJAX吧
解决方案三:
namespaceInternetShopping.AdminManage{publicpartialclassUserRegister:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){stringtxtName=TextBox7.Text;stringnum=("Server=(local);IntegratedSecurity=SSPI;database=bookshopping");SqlConnectionconnect=newSqlConnection(num);stringsql="selecttxtNamefromCustomwheretxtName="txtName";SqlCommandcommand=newSqlCommand(sql,connect);connect.Open();intnumcount=Convert.ToInt32(command.ExecuteScalar());if(numcount.length>0){Response.Write("<script>alert('用户名已存在');</script>");}}
解决方案四:
做一个光标离开事件,然后在事件中查询数据库,这是简单的,无刷新就得用ajax
解决方案五:
引用3楼qq_17634739的回复:
做一个光标离开事件,然后在事件中查询数据库,这是简单的,无刷新就得用ajax
不好意思啊,有木有什么例子额,我初学
解决方案六:
控件有个属性,mouseleave,应该是这样拼,光标重控件中离开触发的事件,你把验证写在这个里面
解决方案七:
protectedvoidButton1_Click1(objectsender,EventArgse){stringUserName=this.TextBox1.Text;TextBox1.Text="";stringnum=("Server=(local);IntegratedSecurity=SSPI;database=bookshopping");SqlConnectionconnect=newSqlConnection(num);stringsql="select*fromCustomwherecustomName='"+UserName+"'";SqlCommandcommand=newSqlCommand(sql,connect);connect.Open();intnum1=Convert.ToInt32(command.ExecuteScalar());if(num1>0){Response.Write("<script>(function(){alert('用户名已存在');})()</script>");}else{SqlDataSource1.Insert();Response.Redirect("BuyComputerBooks.aspx");}connect.Close();}
解决方案八:
引用5楼qq_17634739的回复:
控件有个属性,mouseleave,应该是这样拼,光标重控件中离开触发的事件,你把验证写在这个里面
这个事件坑爹的。。如果你的光标就没有上去过这个事件是不会触发的。。。最妥的办法就是Ajax或者直接后台保存之前校验。