你好,我有問題,每當我按下刷新按鈕,它不斷添加:/我如何防止它?它是我的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 –