|
|
10 Oct 2007
|
Browser Back Button Problem in ASP.Net Application
Browser back button is sometimes a problem to web development. Let us see this scenario.
An application has a page responsible for adding an item to the database. After the add action is completed, the application redirects the user to another page
with a confirmation message. However, some users occasionally use the browser back button to go back to the previous page and press the add button again. As a result, a duplicate record is added to the database.
To protect database from recieving duplicate entry, I add a hidden label in the aspx page. At the page load
evemt, this hidden label is updated with a
new GUID (e.g. lblHiddenGuid.Text = System.Guid.NewGuid.ToString). When writing
data to the database, this GUID is added to it. For every new record, this
GUID is checked against the database. If it is found, the new record is added, else the action is assumed as repeated action and is ignored.
As historical page displays everything including the hidden GUID from the
previous page whereas a new page has a new hidden GUID. We can use this method
to distinguish a new page from a historical page. An "insert stored procedure" may look like this:
CREATE PROCEDURE [dbo].[usp_AddRecord]
(
@RecordID int,
@RecordDate smallDatetime,
@Field1 varchar(100),
@Field2 int,
@FieldGuid varchar(36)
)
AS
/* SET NOCOUNT ON */
declare @count int
Select @count=Count(*) from RecordTable where FieldGuid=@FieldGuid
If @count=0
Insert Into RecrodTable (RecordID, RecordDate,
Field1, Field2, FieldGuid) Values
(@RecordD, @RecordDate,
@Field1, @Field2, @FieldGuid)
RETURN
You can also visit my other blog relating to browser's back button.
|