你好,我有问题,每当我按下刷新按钮,它不断添加:/我如何防止它?它是我的2搞清楚了这一点的一天,我失败了:(防止每当用户点击刷新时添加新记录
protected void Button1_Click(object sender, EventArgs e)
{
//Instantiate the connection with the database
using (SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=password;" +
"trusted_connection=yes;" +
"database=DataBaseConnection;" +
"connection timeout=30;"))
{
//Open the Connection object
myConnection.Open();
//Instantiate a SQL Command with an INSERT query
SqlCommand myCommand = new SqlCommand("INSERT INTO BasicInfo (Firstname,Surname) Values(@a,@b);", myConnection);
if (TextBox1.Text !=null && TextBox2.Text != null)
{
//Texbox
myCommand.Parameters.AddWithValue("@a", TextBox1.Text);
myCommand.Parameters.AddWithValue("@b", TextBox2.Text);
//Execute the query
myCommand.ExecuteNonQuery();
}
else
{
//Code HEre?
}
你不能使用'!IsPostBack()' – FosterZ
@FosterZ:在这种情况下,他不能。 Button1_Click事件由回发触发。 – rsbarro
这里不是一个ASP .NET开发者,但是什么时候'TextBox1.Text'永远是'null'?你的意思是与'String.Empty'进行比较还是使用'String.IsNullOrEmpty'来代替? http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.text.aspx –