2010-02-21 89 views
0

由於某些原因,當用戶點擊提交按鈕並重新刷新頁面時,相同的數據將再次上傳到我的SQL Server 2005數據庫。我不會發生這種事情...........爲什麼會發生這種情況?我正在使用SQL數據源!在ASP.NET頁面刷新時再次頁面上傳數據

我的代碼

Try 

     'See if user typed the correct code. 
     If Me.txtSecurity.Text = Session("Captcha") Then 

      If Session("NotifyMe") = "Yes" Then 
       SendEmailNS() 
      End If 

      RaterRate.Insert() 
      RaterRate.Update() 

      DisableItems() 

      lblResultNS.Text = "Thank you for leaving a comment" 

      LoadCompanyList() 
      LoadRateRecords() 

      txtCommentNS.Text = "" 
      txtSecurity.Text = "" 
      lblResultNS.Focus() 

     Else 

      Session("Captcha") = GenerateCAPTCHACode() 
      txtSecurity.Text = "" 
      txtSecurity.Focus() 
      Validator10.Validate() 

     End If 


    Catch ex As Exception 

     lblResultNS.Visible = True  
     lblResultNS.Text = ex.Message.ToString 
     lblResultNS.Focus() 

    End Try 

回答

0

這是因爲WebForms的依靠回發模式的本質發生。每次點擊一個按鈕時,頁面內的單個表單將與視圖狀態一起發佈到服務器。當您按F5時,您通常會收到警告,表單將被重新發布,如果您單擊是,則會在服務器上執行相同的操作。避免這種情況的一種方法是在保存到數據庫後執行重定向:

Response.Redirect("~/success.aspx"); 
+0

另一種方法是重置表單值或更改表格ID – Adeel 2010-02-21 08:32:56

+0

艾我不想重定向用戶.......... – Etienne 2010-02-21 08:41:08

1

當用戶訪問您的頁面時,生成新的驗證碼。在發佈或發佈後,其檢查驗證碼但不生成新的驗證碼。將'驗證碼集'代碼移至外部作用域。

If Me.txtSecurity.Text = Session("Captcha") Then 
    ... 
Else 
    ... 
End If 

Session("Captcha") = GenerateCAPTCHACode()