2011-02-10 46 views
2

區分beteen「Refresh Post」或「Real Post Back」的最佳方式是什麼?什麼是區分beteen「Refresh Post」或「Real Post Back」的最佳方式

這是我需要獲得

protected void Button1_Click(object sender, EventArgs e) 
{ 

if(PostBack && !Refresh) 
{ 
//Do Something 
} 

} 
+0

您是否在如何避免雙重提交情況(即用戶在提交某些內容後多次進行刷新以降低雙倍或多次保存記錄的出現次數)的情況下提出要求? – 2011-02-10 17:25:24

+0

完全正確。因爲創建原始帖子的事件在刷新期間被解僱。 – Ananth 2011-02-10 19:09:39

回答

1

您可以設置一個隱藏的輸入與每一個加載窗體(但不回發)時隨機生成的隨機數值,然後檢查是否現時值了送了兩次。如果它再次發送,則是刷新。

+0

Thanks BoltClock ...接受此答案,因爲這是問題的正確答案(區分「Refresh Post」或「Real Post Back」)。其他兩個答案都提到了一個很好的解決方案,以避免問題。Back back – Ananth 2011-02-10 19:29:45

3

我通常做一個Response.Redirect到回發事件中的同一頁面。 這樣,我所有的Page.IsPostBack是真實的回傳,而不是刷新

+1

+1和更多關於此模式的信息:http://en.wikipedia.org/wiki/Post/Redirect/Get – Paolo 2011-02-10 10:58:28

1

,你可以嘗試像

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //your code of Click event 
    //.............. 
    //............... 
    // and then add this statement at the end 
    Response.Redirect(Request.RawUrl); // Can you test and let me know your findings 
} 
1

樣品爲接受的答案

工作的代碼添加此行的設計師

<input type="hidden" runat="server" id="Tics1" value="GGG" /> 

在代碼後加上以下代碼

public partial class WebForm1 : System.Web.UI.Page 
{ 

    long tics = DateTime.Now.Ticks; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      this.Tics1.Value = tics.ToString(); 
      Session["Tics"] = tics; 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (Session["Tics"] != null && Request["Tics1"] != null) 
     { 
      if (Session["Tics"].ToString().Equals((Request["Tics1"].ToString()))) 
      { 
       Response.Write("Postback"); 
      } 
      else 
      { 
       Response.Write("Refresh"); 
      } 
     } 
     this.Tics1.Value = tics.ToString(); 
     Session["Tics"] = tics;   
    } 
} 
相關問題