469,917 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Prevent double submit of asp.net v2.0 form

Hi all.

I have a problem in my website in trying to prevent a user from double submitting the form. I am calling a subroutine from my page_load event that passes in the commandbutton that I want to prevent users from being able to click twice. Everything is working great, except for one small detail. When my client-side validation fires, and is invalid, I see the red text that displays the error message, but the problem is that something is "stuck" and I cannot cause a postback for 1 click.. I will give 2 examples of what I mean by this:

1) I press submit and my client-side validation tells me that I need to enter the password, but at this point, I click the cancel button, which should take me to a previous screen, but instead, all it does is get rid of the validation text that tells me to enter the password. When I click cancel AGAIN, it works great.. Basically I have to press cancel twice in this case.

2) This is the real problem I have had with this: After validating a dropdown, the validation errormsg says I need to select a value from the dropdown before submitting. OK, I do this, on a dropdown that has autopostback=true set, but IT DOES NOT POSTBACK. This is very bad because I have logic on the onselectedindexchange event for this dropdown, and it now doesn't fire. Of course, if I re-select another dropdown value at this time, then everything is great, and it postsback just fine.

I hope the code below will be enough information to duplicate, and/or help me determine what I am doing wrong. I can also dumb the code down if need be, so that it only disables the submit button for example. Also, if need be, I can post a small sample asp.net application later that may help you to see the problem.

Here is the code:

Public Sub PreventDoubleSubmit(ByRef cmd As System.Web.UI.WebControls.Button)
Dim sb As New System.Text.StringBuilder

ClientScript.GetPostBackClientHyperlink(cmd, "")

sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")
sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
sb.Append("scroll(0,0);")
sb.Append("var outerPane = document.getElementById('FreezePane');")
sb.Append("var innerPane = document.getElementById('InnerFreezePane');")
sb.Append("if (outerPane) outerPane.className = 'FreezePaneOn';")
sb.Append("if (innerPane) innerPane.innerHTML = 'Please wait...';")

cmd.Attributes("onclick") = sb.ToString()
End Sub

It is called from page_load as follows:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PreventDoubleSubmit(Me.cmdSend)
End Sub

there is also a couple divs in the html that are needed and referenced by the javascript to basically pop up a 'Please wait' message on successful validation and submit:

<div align="center" id="FreezePane" class="FreezePaneOff">
<div id="InnerFreezePane" class="InnerFreezePane"> </div>
</div>

Finally, here are a few css styles to make it look nice:

.FreezePaneOff {
visibility: hidden;
display: none;
position: absolute;
top: -100px;
left: -100px;
}

.FreezePaneOn {
position: absolute;
top: 0px;
left: 0px;
visibility: visible;
display: block;
width: 100%;
height: 100%;
background-color: #666;
z-index: 999;
filter:alpha(opacity=85);
-moz-opacity:0.85;
padding-top: 20%;
}

.InnerFreezePane {
text-align: center;
width: 66%;
background-color: #efefef;
color: black;
font-size: large;
border: dashed 2px #111;
padding: 9px;
}
Mar 27 '07 #1
0 5910

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by entoone | last post: by
6 posts views Thread by Lloyd Dupont | last post: by
3 posts views Thread by Anders K. Jacobsen [DK] | last post: by
1 post views Thread by rmgalante | last post: by
14 posts views Thread by Ed Jay | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.