Default Button not working after Validation

R

Remy

Here is the situation:
I have an ASP.NET 2.0 page with 2 panel. Each has a few textboxes and
one DefaultButton. All textboxes have one or more Validator.
Now everything works fine if I fill everything out correctly and then
press the Enter Key, but if the validation fails and I fix the entry
and press enter again, nothing happens anymore.

Someone had the same issue?

Remy
 
P

paul.hine

Yes, I am having the same problem. Looks like it's back to to the
manual approach until MS releases a fix.

-paul
 
P

paul.hine

Ok, I did some poking around and found the problem in the MS
WebResource.axd javascript. To prevent the form from posting multiple
times when an impatient or clumsy user presses enter more than once, MS
sets a flag when the FireDefaultButton function is called. This does
not get reset if the form fails validation, so FireDefaultButton
ignores all subsequent Enter Key presses.

What MS should do in their next hotfix (are you listening?) is set the
__defaultFired flag to true as the last step in the form.onsubmit(), so
that it only prevents the Enter Key press from submitting when it will,
in fact, cause duplicate posts.

Better yet, MS should add a property to the Button controls called
"AllowMultiplePostBacks", defaulted to false. Might be worth doing this
in a custom control in the meantime...

Anyway, there is a workaround. It is a "hack" because the
__defaultFired flag is not intended to be tweaked in client code and
may change in future releases, but it works for now. Keep in mind that
after installing this hack, the form will submit multiple times if the
Enter Key is pressed repeatedly unless you put something in your page
to prevent that.

Put the following code somewhere in the page. Be sure to replace
[DefaultButtonValidationGroup] with the validation group set on your
DefaultButton, or remove that param if you are not using validation
groups.

<script type="text/javascript" language="javascript">
function defaultButtonFix() {
__defaultFired = false;
}
</script>
<asp:CustomValidator ID="cvDefaultButtonFix" runat="server"
ClientValidationFunction="defaultButtonFix"
Display="None"
ValidationGroup="[DefaultButtonValidationGroup]" />
 
R

Remy

Hey Paul
Thanks a lot for the hint, that was worth a lot and worked nicely, but
after some more digging around I found a second approach that does not
have the repeatedly pressed Enter button problem.
I just "override" the asp.net javascript function and set the
__defaultFired to false if the validation fails.
Its also a pretty big hack, but it seems to work in IE at least.

<script type="text/javascript" language="javascript">
//overwrites the original Page_ClientValidate
function Page_ClientValidate(validationGroup) {
Page_InvalidControlToBeFocused = null;
if (typeof(Page_Validators) == "undefined") {
return true;
}
var i;
for (i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators, validationGroup, null);
}
ValidatorUpdateIsValid();
ValidationSummaryOnSubmit(validationGroup);
Page_BlockSubmit = !Page_IsValid;
//this is to correct a little bug in ASP
if(!Page_IsValid)
__defaultFired = false;

return Page_IsValid;
}
</script>

Cheers
Remy Blaettler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top