GridView_RowCommand event fired again on update page

I have a GridView Row editing function in my .net application using <asp:CommandField . I clicked the Refresh button to save the record after editing the line. Now, if I refresh the page or click F5 GridView_RowCommand again.

How can we avoid this. Is there any mechanism for identifying when the user presses F5 or refreshes the page. Is there any method on the client side or on the server side.

+4
source share
3 answers

Not exactly the best β€œtechnical” solution to your problem, but you can always just do Response.Redirect(Request.RawUrl) after you have finished everything you need to do in your RowCommand

As I said, this is not the best "technical" solution, but this solution

Dave

+5
source
 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. } } 
+1
source

One way to capture it is to maintain a session variable associated with the page in question. In the session variable, you must save some state of the enumeration, key or string, which would determine the last action taken. You can even use a simple counter with an addition, and if you have ever received a counter counter equal to a session variable, this indicates a page refresh, not a new action.

+1
source

Source: https://habr.com/ru/post/1337467/


All Articles