Persits Software, Inc. Knowledge Base Articles

When hitting Refresh button, another record is added to the table

Problem Description

After adding a new record with AspGrid, if a user hits the Refresh button on the browser, another identical record is added to the table.

Solution

To prevent an unwanted record from being added to the table when the Refresh button is hit, a session variable may be used to store the last query string content. If the current query string value is the same as the previous value, it means that the Refresh button was hit, and no updates should be performed.

The following code snippet demonstrates the technique:

<%
...
' concatenate together names and values from Query String
For Each Item in Request.QueryString
   Set SubItem = Request.QueryString(Item)
   NewString = NewString & Item
   For i = 1 To SubItem.Count
      NewString = NewString & SubItem(i)
   Next
Next

' Compare new query string with previos one
If NewString = Session("TheQueryString") Then
    Grid.IgnoreCommands
' Do not update
End If

' Save new query string
Session("TheQueryString") = NewString

Grid.Display
%>

If your grid uses the POST method (the property MethodGet is set to False) the code above must use Reqest.Form instead of Request.QueryString, as follows:

<%
...
Grid.MethodGet = False
...
For Each Item in Request.Form
   Set SubItem = Request.Form(Item)
   ...
Next

...
%>