2011-01-28 94 views
4

我在.net應用程序中使用<asp:CommandField實現了GridView行編輯功能。 我點擊更新按鈕來保存編輯行後的記錄。現在如果我刷新頁面或按F5 GridView_RowCommand再次發射。在頁面刷新時再次觸發GridView_RowCommand事件

我們該如何避免這種情況。當用戶按F5或刷新頁面時,是否有任何機制可以識別。是否存在客戶端或服務器端的任何方法。

回答

5

不正是最好的「技術」的解決您的問題,但你總是可以只是做一個Response.Redirect(Request.RawUrl)當你完成做任何事情,你需要在你的RowCommand

做就像我說的,這不是最好的「技術「解決方案,但它是一個解決方案

戴夫

1
Session["LastInsertedItem"] = null; 
MyCustomObjType myCustomObject; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    myCustomObject = Session["LastInsertedItem"] as MyCustomObjType; 
} 
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e) 
{ 
    If(myCustomObject == null || //!(compare your value with myCustomObject.field)) 
    { 
     // do your operations and save the values to myCustomObject and save that object back to Session. 
    } 
    else 
    { 
     // It is refreshed or same data is being insterted - don't know if second   option is possible in your case. 
    } 
} 
+0

我得到的IsPostBack =真當我點擊更新按鈕和頁面刷新也。 – Sukhjeevan 2011-01-28 12:23:43

+0

當我刷新頁面我得到了Ispostback = true和數據再次保存,我已經保存在updatebutton點擊。 – Sukhjeevan 2011-01-28 12:35:38

1

捕獲此問題的一種方法是維護與相關頁面相關的會話變量。在會話變量中,您將保留某種狀態枚舉,鍵或字符串來確定最後採取的操作。你甚至可以使用一個簡單的遞增計數器,並且如果你曾經收到一個與會話變量相同的回傳計數器,它將表示一個頁面刷新,而不是一個新的動作。

相關問題