4

I have implement GridView Row Editing feature in my .net application using <asp:CommandField. I clicked on Update button to save the record after editing the row.Now if i refresh the page or press F5 GridView_RowCommand fired again.

How can we avoid this.Is there any mechanism to identify when user press F5 OR refresh the page.Is there any method in client side or in server side.

Sukhjeevan
  • 3,288
  • 7
  • 41
  • 84

3 Answers3

5

Not exactly the best "technical" solution to your problem but you could always just do a Response.Redirect(Request.RawUrl) once you have finished doing anything you need to do in your RowCommand

Like I said, it's not the best "technical" solution but it is a solution

Dave

Dave
  • 583
  • 1
  • 4
  • 11
  • Thanks Dave.This solution really worked for me.It is something like that if i write in RowCommand [ Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "k", "window.location.href=window.location.href", True) ] i.e. after RowCommand code get executed when we refresh the page asp.net process consider it as a GET request that's why on refresh no pop up opens for asking retry or cancel.Well this solution worked in my case. Thanks a lot. – Sukhjeevan Feb 09 '11 at 13:03
  • That's pretty much the same kind of thing but achieved through your JavaScript - glad it worked for you :) – Dave Feb 09 '11 at 13:58
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.
    }
}
Pabuc
  • 5,430
  • 6
  • 33
  • 51
  • i got IsPostback = true when I click on Update button and on page refresh also. – Sukhjeevan Jan 28 '11 at 12:23
  • when i refresh the page i got Ispostback = true and data again saved that i already saved on updatebutton clicked. – Sukhjeevan Jan 28 '11 at 12:35
  • Keep an object in session with the values you inserted last and check if it is same with the last inserted values. If so, simply return. I'm editing my answer. – Pabuc Jan 28 '11 at 12:37
1

One method of capturing this is to maintain a session variable that is related to the page in question. In the session variable you would keep some kind of state enumeration, key or string that would determine the last action taken. You could even use a simple incremented counter, and if you ever received a postedback counter that was equal to the session variable it would indicate a page refresh rather than a new action.

Joel Etherton
  • 36,043
  • 10
  • 81
  • 99