2014-10-26 122 views
0

我搜索了很多..但未能實現。 這裏是我的代碼,我如何防止重複插入頁面刷新,,(我如何使用viewstate,或任何其他方法.... resposnse.redirect不是在這種情況下的解決方案)。防止頁面刷新時出現重複記錄插入

protected void btn_save_click(object sender, EventArgs e) 
    { 
      System.Data.DataTable dt_plotid = new System.Data.DataTable(); 
      dt_plotid = FghBAL.Admitting.GetMax_ByPlotID(txt_plot.Text); 
      string plotid = dt_plotid.Rows[0]["ID"].ToString(); 
      if (FghBAL.Alotee.InserAllotee(txt_alotee_name.Text.ToUpper(), ddl_alotee_sw.SelectedValue.ToUpper(), txt_alotee_fname.Text.ToUpper(), alotee_cnic, txt_alotee_phone.Text, txt_alotee_cellno.Text, txt_alotee_address.Text, txt_alotee_email.Text, plotid)) 
      { 
       lbl_error_3.Visible = true; 
       lbl_error_3.Text = "Add Successfully"; 
       txt_alotee_name.Enabled = false; 
       ddl_alotee_sw.Enabled = false; 
       txt_alotee_fname.Enabled = false; 
       txt_alotee_cnic_1.Enabled = false; 
       txt_alotee_cnic_2.Enabled = false; 
       txt_alotee_cnic_3.Enabled = false; 
       txt_alotee_phone.Enabled = false; 
       txt_alotee_cellno.Enabled = false; 
       txt_alotee_address.Enabled = false; 
       txt_alotee_email.Enabled = false; 
       System.Data.DataTable dt_alotee_data = new System.Data.DataTable(); 
       dt_alotee_data = FghBAL.Alotee.GetDatabyPlot_Id(plotid); 
       grid_alotee.DataSource = dt_alotee_data; 
       grid_alotee.DataBind(); 
       grid_alotee.Visible = true; 
       btn_save_alotee.Visible = true; 

       btn_link_another_alotee.Visible = true; 
       lbl_grd_alotee.Visible = true; 
       tabcontent3.Style.Add("height", "80%"); 
       //ViewState["dt_alotee_view"] = dt_alotee_data; 
       btn_save_alotee.Enabled = false; 
       // Response.Redirect(Request.Url.AbsoluteUri); 
      } 
      else 
      { 
       lbl_error_3.Text = "Unable to add Allottee Information"; 
      } 
     } 
+0

* resposnse.redirect在這種情況下不是解決方案*爲什麼? – 2014-10-26 14:38:16

+0

,因爲它移動到第一個標籤..但我希望它留在當前標籤..即時通訊在同一頁上使用多個標籤(標籤由單選按鈕控制) – 2014-10-26 14:40:35

+0

在InserAllotee方法中添加一個重複的檢查邏輯。 – 2014-10-26 14:42:18

回答

1

你的問題似乎表明一個按鈕事件處理程序觸發頁面刷新 - 這是奇怪的。頁面刷新導致GET操作。儘管如此,服務器端數據的更改只能在POST請求中完成。而按鈕事件處理程序應該只在單擊按鈕時調用。我無法從這些代碼中得知您的架構違反了這些規定。

+0

我調試代碼時刷新頁面它不去btn_save_click()但記錄插入db重複 – 2014-10-26 15:15:43

+0

如果它不去這個函數,記錄不是從此功能插入(或不在該時間點)。除了您發佈的代碼之外,還有其他插入代碼嗎? – 2014-10-26 15:16:59

+0

我確定沒有 – 2014-10-26 15:47:55