Preventing double post by refreshing?

  • Thread starter Thread starter Jim in Arizona
  • Start date Start date
J

Jim in Arizona

How can I prevent a second (or third or fourth) post into a database by
someone clicking the refresh button on their browser?

Basically, the user clicks a button called "New Log", which makes most
of the form within a <div runat="server"> to go invisible and another
<div> go visible. This includes a text box and a save button. They type
text into the textbox and click save, which executes a stored procedure
that does the insert into an SQL database. Then that <div> goes
invisible and the original <div> goes visible. This all works fine but
if they hit the refresh button, another, exact post is supplied (with a
new time stamp since I also insert Now().

I tried this:

If txtNewLog.Text = "" Then

Else
objConnection.Open()
strNameUsers = objCommandUsers.ExecuteScalar()
objName.Value = strNameUsers
objCommand.ExecuteNonQuery()
objConnection.Close()

txtNewLog.Text = vbNullString
divPost.Visible = False
PopulateList("")
divNotes.Visible = True
txtNewLog.Text = ""
End If

Note that I try to clear the contents of the text box after the insert
is complete. I was hoping that if they hit refresh, the insert wouldn't
take place with this coding, but it does.

TIA,
Jim
 
Milosz said:
Hi Jim,

You can implement IsRefersh property, but you need both viewstate and
session to be enabled, C# example:

Since I've never had to deal with turning on or off the viewstate or
session, I dont' know how to do that. I'll look into it.

I definitely don't know c sharp but I'm sure I can make out your code
with a little focus and head scratching. ;)

Thanks for your response. I didn't know there was a 'IsRefresh' until
now so, armed with that, I'll give it a go.
 
Jim said:
Since I've never had to deal with turning on or off the viewstate or
session, I dont' know how to do that. I'll look into it.

I definitely don't know c sharp but I'm sure I can make out your code
with a little focus and head scratching. ;)

Thanks for your response. I didn't know there was a 'IsRefresh' until
now so, armed with that, I'll give it a go.

This code converter worked, apparently.

http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

With this result:

Private Const refLockCountFieldName As String = "__refLockCount"

Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)
Page.RegisterHiddenField(refLockCountFieldName,
SessionLockUpdateCount.ToString)
MyBase.Render(output)
End Sub
Private isRefreshLock As Boolean = False
Private Const initialLockUpdateValue As Integer = 1

Private Sub EnsureRefreshLock()
If isRefreshLock Then
Return
End If
If Not IsPostBack Then
SessionLockUpdateCount = initialLockUpdateValue
End If
Dim count As Integer = HdnLockUpdateCount + 1
If count > SessionLockUpdateCount Then
SessionLockUpdateCount = count
Else
isRefresh = True
End If
isRefreshLock = True
End Sub
Private isRefresh As Boolean = False

Public ReadOnly Property IsRefresh() As Boolean
Get
EnsureRefreshLock
Return isRefresh
End Get
End Property

Protected ReadOnly Property HdnLockUpdateCount() As Integer
Get
Dim str As String = Request(refLockCountFieldName)
Dim count As Integer = initialLockUpdateValue
If Not (str Is Nothing) AndAlso str.Length > 0 Then
Try
count = Int32.Parse(str)
Catch
End Try
End If
Return count
End Get
End Property

Protected Property SessionLockUpdateCount() As Integer
Get
Dim obj As Object = Session("LockUpdate")
Return Microsoft.VisualBasic.IIf(obj Is
Nothing,initialLockUpdateValue,CType(obj, Integer))
End Get
Set
Session("LockUpdate") = value
End Set
End Property
 
Milosz said:
Hi again,

Both session and viewstate are enabled by default, i mentioned this, in case
you might have set page's viewstate to false or turned session state off.
Sorry i posted c# version but i didn't have time to translate it to vb.net
(you'll be fine :)

hope this helps

I couldn't get that code to work after I tried converting it. I did find
this article:

http://aspalliance.com/687

It happens to be in VB too, which was great for me. I found that, in my
case, all I had to do was use this within my button sub:

Response.Redirect(Request.Url.ToString(), false)

That works fine since maintaining viewstate isn't necessary on that page.

Thanks,
Jim
 
Back
Top