2013-02-10 41 views
0

我在頁面中有一個DropDownlist和一個文本框,當我點擊按鈕時,Button1_Click事件觸發並獲取od文本框和droppdownlist值並將它們發送到我在數據庫中的表,它顯示了gridview中的結果(數據庫中我的表中新添加的行)。 我的問題是我點擊按鈕後,如果我刷新我的網頁我的意思是點擊重新加載當前頁面,我的下拉列表值和文本框值再次插入我的表中。 我不知道如何解決它? 我的GridView使用SqlDataSource和這是我的代碼:我想要我的控件值。只有一次當我點擊按鈕

protected void Button1_Click(object sender, EventArgs e) 
    { 
    int brandid = Convert.ToInt32(BrandDropDownList.SelectedValue); 
    String catname = TextBox2.Text; 
    SqlConnection cn = new SqlConnection(); 
    cn.ConnectionString = "server = . ; database = mobile_store ; Trusted_Connection=true"; 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = cn; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "FirstInitialiseCategories"; 
    cmd.Parameters.AddWithValue("@brandid ", brandid); 
    cmd.Parameters.AddWithValue("@catname ", catname); 
    try 
    { 
     cn.Open(); 
     cmd.ExecuteNonQuery(); 
     Label1.ForeColor = System.Drawing.Color.Green; 
     Label1.Text = "با موفقیت ثبت شد!"; 
     SqlDataSource2.DataBind(); 

     GridView1.DataBind(); 
    } 

    catch (Exception ex) 
    { 
     Label1.Text = ex.ToString(); 
     Label1.ForeColor = System.Drawing.Color.Red; 
     Label1.Text = "خطا در ثبت!"; 
    } 
    finally 
    { cn.Close(); } 
} 

回答

2

你可以做一個HTTP重定向(在同一個頁面),一旦你與你的第一次插入完成後,它應該解決的問題:

Response.Redirect("samePage.aspx"); 
+0

非常感謝你,它現在正常工作 – 2013-02-10 18:33:44

+0

很高興它做到了,隨時接受它作爲答案:) – 2013-02-10 18:35:10

0

下拉列表中的第一項是什麼?您可以爲第一項設置一個空值,並且當您向數據庫中插入值時,可以添加代碼以清除文本框和下拉列表。

finally { cn.Close(); Label1.Text =「」; ddl.SelectedIndex = 0; }

+0

謝謝你的回答,我曾嘗試過,但它沒有工作 – 2013-02-10 18:33:23