AutoPostback and validators

H

Hans Kesting

Hi,

I have a pulldown with both AutoPostback set to true and a validator.
When I select an "invalid" value from the pulldown, the validator message
shows briefly, then a postback occurs and the errormessage is gone!

Why doesn't the validator prevent autopostback (or can I set something so
that it *will* prevent postback)?

Background:
The pulldown contains "groupheaders" and "groupitems". The user should only
select groupitems, so I use a validator to prevent headers from being selected.
First I used a RequiredFieldValidator (all "headers" had a value "--", which was
also set as "InitialValue" for the validator), then a CompareValidator ("headers"
changed to negative values and the validator checked for positive value).
Both did block when I clicked the "save" button. Neither prevented the
postback so the error flashed briefly before being removed.

Checking for "IsValid" (either on Page or on the validator) has no effect,
it's the postback itself that causes the message to disappear.


Is there someway to prevent the postback if the validator fails, or retain the
validator message after postback?


Hans Kesting
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

The CausesValidation property will help here. Unfortuanately, it is an all
or nothing proposition under 1.x. WIth 2.0, you get validation groups.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
H

Hans Kesting

Cowboy (Gregory A. Beamer) said:
The CausesValidation property will help here. Unfortuanately, it is an all
or nothing proposition under 1.x. WIth 2.0, you get validation groups.

A DropDownList doesn't have a CausesValidation property!
The validation occurs (I can see a red text coming up) but then the autopostback
will occur, regardles of the "IsValid" state of that validator.
After the postback the validator is cleared so I don't see the errormessage,
until I try to "save" this record.

If the validator signals an error, then either the autopostback should not occur,
or the message should remain visible after the postback roundtrip.

Hans Kesting
 
B

bruce barker

if your validation is displaying, but submit happens anyway when you click
on a asp:button (rendered as a submit) this is caused by an IE 6.0 bug. it
can be fixed with javascript code, but you will have to write your own
validatoirs or change the WebUIValidation.js file.

a little background. some versions of 6.0 will postback when they shouldn't.
the following html should never postback

<script>function Validate(){return false;}</script>
<input type=submit value="should not post" onclick="return Validate();">

but with some IE's the postback will occur anyway (actually depends on other
html elements).

the following code will fix it.

<script>function CancelPostback() {if (document.all) event.returnValue =
false; return false;}</script>
<script>function Validate(){return CancelPostback;}</script>
<input type=submit value="should not post" onclick="return Validate();">

I don't use the builtin validation rutines but try editing
WebUIValidation.js, change

function Page_ClientValidate() {
var i;
for (i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators);
}
ValidatorUpdateIsValid();
ValidationSummaryOnSubmit();
Page_BlockSubmit = !Page_IsValid;
event.returnValue = !Page_IsValid; // force IE to honor return value
return Page_IsValid;
}


-- bruce (sqlwork.com)
 
H

Hans Kesting

Bruce,

Thanks for your answer.

I don't think I want to change anything in these functions ( too
much chance for deployment errors), so I guess I have to live
with it.
Anyway, my situation is slightly different from your description:
the postback from the submit-button is correctly blocked, it's the
autopostback from a pulldown that happens no matter what
the validator says.

Hans Kesting

bruce barker said:
if your validation is displaying, but submit happens anyway when you click
on a asp:button (rendered as a submit) this is caused by an IE 6.0 bug. it
can be fixed with javascript code, but you will have to write your own
validatoirs or change the WebUIValidation.js file.

a little background. some versions of 6.0 will postback when they shouldn't.
the following html should never postback

<script>function Validate(){return false;}</script>
<input type=submit value="should not post" onclick="return Validate();">

but with some IE's the postback will occur anyway (actually depends on other
html elements).

the following code will fix it.

<script>function CancelPostback() {if (document.all) event.returnValue =
false; return false;}</script>
<script>function Validate(){return CancelPostback;}</script>
<input type=submit value="should not post" onclick="return Validate();">

I don't use the builtin validation rutines but try editing
WebUIValidation.js, change

function Page_ClientValidate() {
var i;
for (i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators);
}
ValidatorUpdateIsValid();
ValidationSummaryOnSubmit();
Page_BlockSubmit = !Page_IsValid;
event.returnValue = !Page_IsValid; // force IE to honor return value
return Page_IsValid;
}


-- bruce (sqlwork.com)


Hans Kesting said:
Hi,

I have a pulldown with both AutoPostback set to true and a validator.
When I select an "invalid" value from the pulldown, the validator message
shows briefly, then a postback occurs and the errormessage is gone!

Why doesn't the validator prevent autopostback (or can I set something so
that it *will* prevent postback)?

Background:
The pulldown contains "groupheaders" and "groupitems". The user should only
select groupitems, so I use a validator to prevent headers from being selected.
First I used a RequiredFieldValidator (all "headers" had a value "--", which was
also set as "InitialValue" for the validator), then a CompareValidator ("headers"
changed to negative values and the validator checked for positive value).
Both did block when I clicked the "save" button. Neither prevented the
postback so the error flashed briefly before being removed.

Checking for "IsValid" (either on Page or on the validator) has no effect,
it's the postback itself that causes the message to disappear.


Is there someway to prevent the postback if the validator fails, or retain the
validator message after postback?


Hans Kesting
 
H

Hans Kesting

Hans Kesting said:
Hi,

I have a pulldown with both AutoPostback set to true and a validator.
When I select an "invalid" value from the pulldown, the validator message
shows briefly, then a postback occurs and the errormessage is gone!

Why doesn't the validator prevent autopostback (or can I set something so
that it *will* prevent postback)?

Background:
The pulldown contains "groupheaders" and "groupitems". The user should only
select groupitems, so I use a validator to prevent headers from being selected.
First I used a RequiredFieldValidator (all "headers" had a value "--", which was
also set as "InitialValue" for the validator), then a CompareValidator ("headers"
changed to negative values and the validator checked for positive value).
Both did block when I clicked the "save" button. Neither prevented the
postback so the error flashed briefly before being removed.

Checking for "IsValid" (either on Page or on the validator) has no effect,
it's the postback itself that causes the message to disappear.


Is there someway to prevent the postback if the validator fails, or retain the
validator message after postback?


Hans Kesting

Found a (temporary?) solution:
In the SelectedIndexChanged server-side eventhandler, check for the error condition
and set the IsValid of that validator accordingly.
The errormessage now "remains" visible and a submit is still blocked.

Hans Kesting
 

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

Similar Threads

Validators and JavaScript 1
AJAX and validators 2
Dynamic Validators 0
Dynamic Validators 0
Having problems with validators 7
multiview and validators 0
Validators 1
Validators & ICallbackEventHandler 1

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top