Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Resetting Validation Controls via Reset Button

Reply
Thread Tools

Resetting Validation Controls via Reset Button

 
 
NancyASAP
Guest
Posts: n/a
 
      03-11-2005
Thought I'd share this since it took me a long time to get it working.
Thanks to a bunch of contributers in Google Groups who shared
javascript, etc.

The question was: How can I put a reset button on my ASP.NET web page,
and have an HTML reset button click clear 1) all validator text display
and 2) validation summary display. Problem was clearing them and yet
still leaving them working and visible if the user immediately began
re-entering data after RESET.

I did it like this:
1) Put HTML Reset button on form.
2) Call a javascript function on Reset button's client-side onclick
event.
3) In the javascript function, reset the appropriate controls.

Note that this works great in Internet Explorer 6.0. However, there are
issues in Mozilla (I tested version 1.7.3). Validation text and summary
still clear, but because of the way that ASP.NET does postback on
submit, Mozilla can't find the original values. Even a plain RESET
button doesn't work right after postback using Mozilla (I tried).
Didn't test Netscape or Firefox.

Reset button:
<INPUT id="btnReset"
onclick="ResetValidators('ValidationSummary1','val ');" type="reset"
value="Clear Form">

Javascript function:
//************************************************** **
// ResetValidators(validationSummaryId, validationControlsPrefix)
// Purpose: Resets ASP.NET validation control text display
// AND validation summary control display to empty.
// Inputs: validationSummaryID - string - id of ValidationSummary
// control
// validationControlsPrefix - string - a prefix to use
// to identify validation controls when looping through all
// span Ids. (Validation controls are represented by spans).
// Outputs: none
//************************************************** **

function ResetValidators(validationSummaryId, validationControlsPrefix)
{
var spans;
var divValSummary;

if (document.all) {
spans = document.all.tags('span');
divValSummary = document.all(validationSummaryId);
}
else if (document.getElementsByTagName) {
spans = document.getElementsByTagName('span');
divValSummary = document.getElementsById(validationSummaryId);
}
if (spans) {
for (var i = 0; i < spans.length; i++) {
var prefixLength = "" + validationControlsPrefix.length;
var currID = "" + spans[i].id
if ((currID != '') && (prefixLength != '')) {
if (currID.substring(0,prefixLength) == validationControlsPrefix) {
//note - set visibility to hidden, not display=none.
// otherwise validator text will never show up again after reset.
spans[i].style.visibility='hidden';
}
}
}
}
if (divValSummary) {
// note set display=none, NOT visibility=hidden. Exact opposite of
// spans above!! Otherwise, validation summary is hidden permanently.
divValSummary.style.display='none';
}

}

Hope this code is helpful.
Nancy Steinmann
MCSD .NET

 
Reply With Quote
 
 
 
 
William F. Robertson, Jr.
Guest
Posts: n/a
 
      03-14-2005
My "reset" button, just re-requests the page. Would this work for you?

<input type="button" onclick="javascript: location.href = location.href;"
value="reset" />

I found it easier to do this, rather than try to clear textboxes, reset drop
down, validators, etc.

HTH,

bill



"NancyASAP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Thought I'd share this since it took me a long time to get it working.
> Thanks to a bunch of contributers in Google Groups who shared
> javascript, etc.
>
> The question was: How can I put a reset button on my ASP.NET web page,
> and have an HTML reset button click clear 1) all validator text display
> and 2) validation summary display. Problem was clearing them and yet
> still leaving them working and visible if the user immediately began
> re-entering data after RESET.
>
> I did it like this:
> 1) Put HTML Reset button on form.
> 2) Call a javascript function on Reset button's client-side onclick
> event.
> 3) In the javascript function, reset the appropriate controls.
>
> Note that this works great in Internet Explorer 6.0. However, there are
> issues in Mozilla (I tested version 1.7.3). Validation text and summary
> still clear, but because of the way that ASP.NET does postback on
> submit, Mozilla can't find the original values. Even a plain RESET
> button doesn't work right after postback using Mozilla (I tried).
> Didn't test Netscape or Firefox.
>
> Reset button:
> <INPUT id="btnReset"
> onclick="ResetValidators('ValidationSummary1','val ');" type="reset"
> value="Clear Form">
>
> Javascript function:
> //************************************************** **
> // ResetValidators(validationSummaryId, validationControlsPrefix)
> // Purpose: Resets ASP.NET validation control text display
> // AND validation summary control display to empty.
> // Inputs: validationSummaryID - string - id of ValidationSummary
> // control
> // validationControlsPrefix - string - a prefix to use
> // to identify validation controls when looping through all
> // span Ids. (Validation controls are represented by spans).
> // Outputs: none
> //************************************************** **
>
> function ResetValidators(validationSummaryId, validationControlsPrefix)
> {
> var spans;
> var divValSummary;
>
> if (document.all) {
> spans = document.all.tags('span');
> divValSummary = document.all(validationSummaryId);
> }
> else if (document.getElementsByTagName) {
> spans = document.getElementsByTagName('span');
> divValSummary = document.getElementsById(validationSummaryId);
> }
> if (spans) {
> for (var i = 0; i < spans.length; i++) {
> var prefixLength = "" + validationControlsPrefix.length;
> var currID = "" + spans[i].id
> if ((currID != '') && (prefixLength != '')) {
> if (currID.substring(0,prefixLength) == validationControlsPrefix) {
> //note - set visibility to hidden, not display=none.
> // otherwise validator text will never show up again after reset.
> spans[i].style.visibility='hidden';
> }
> }
> }
> }
> if (divValSummary) {
> // note set display=none, NOT visibility=hidden. Exact opposite of
> // spans above!! Otherwise, validation summary is hidden permanently.
> divValSummary.style.display='none';
> }
>
> }
>
> Hope this code is helpful.
> Nancy Steinmann
> MCSD .NET
>



 
Reply With Quote
 
 
 
 
parndt parndt is offline
Junior Member
Join Date: Oct 2007
Posts: 1
 
      10-16-2007
call the below script e.g.
someButton.OnClientClick="unvalidate('" + validationSummary.ValidationGroup + "');return false;"; on your codebehind to stop validators' display.

Note that if the validator is set to Display="Static" or Display="Dynamic" it produces either display="inline" or visibility="visible" on the style property in HTML so this has been checked on line 11/13 of the function.

Script to place in your webpage / external javascript file (probably dont need the script tags if its in an external file)

<script type="text/javascript">
<!--
function unvalidate(myValidationGroup)
{
// Remove the validator control(s) from display.
var myValidators = Page_Validators;
if ((typeof(myValidators) != "undefined") && (myValidators != null))
{
for (i=0;i<myValidators.length;i++)
{
var myValidator = myValidators[i];
if (myValidationGroup == null || IsValidationGroupMatch(myValidator, myValidationGroup))
{
if (myValidator.style.visibility.length > 0 && myValidator.style.display.length == 0)
{
myValidator.style.visibility = 'hidden';
}
else if (myValidator.style.display.length > 0 && myValidator.style.visibility.length == 0)
{
myValidator.style.display = 'none';
}
}
}
}

// Remove the validator summary(ies) from display.
var mySummaries = Page_ValidationSummaries;
if ((typeof(mySummaries) != "undefined") && (mySummaries != null))
{
for(i=0;i<mySummaries.length;i++)
{
var mySummary = mySummaries[i];
if (myValidationGroup == null || IsValidationGroupMatch(mySummary, myValidationGroup))
{
mySummary.style.display = 'none';
}
}
}
}
//-->
</script>

NOTE: This relies on the .NET validator client side functions to do anything.. which *should* be present if you have cilent side validation on. No guarantees on functionality but would love to hear feedback on improvements/bugs.

You can call this with just unvalidate(); if you want it to hunt down controls and summaries across _all_ validation groups.

Hope this helps someone, it was driving me mad for a while.
Tested in Firefox 2.0.0.7 & should work in other browsers but let me know if not..

Cheers,
Phil
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reset button and validation controls Jay ASP .Net 1 02-19-2007 10:42 PM
Pix is resetting inbound connections (Reset-I) Aaargh Cisco 4 08-23-2004 05:54 PM
Re: Pix is resetting inbound connections (Reset-I) Kevin Widner Cisco 0 08-20-2004 03:58 PM
Re: Pix is resetting inbound connections (Reset-I) k_widner@yahoo.com Cisco 1 08-20-2004 01:09 PM
HTML Reset Button Doesn't work after postback with Submit button Chris Lane ASP .Net 4 11-17-2003 11:52 PM



Advertisments