Having problems with validators

A

Adrian Parker

Having a problem with validators.

Because we want to only test whether the current field in a changed event is valid before we process the change code, I have tried
to turn off all validators for the page (all ctrls are in a table), then just enable the one relevant validator, run the
page.validate and then if valid, do the change code. otherwise, skip the change code and then finally enable all the validators
again.

What's happening is that it all seems to work except that if the field fails validation, the page isn't displaying the error
message.. until I change the field again. So on first postback, the error doesn't display, but on 2nd postback it does.

The validator does contain the expected error message and IsValid value after each postback.

'-----------------------------------------------
Public Sub ToggleValidators(ByVal bToggle As Boolean)
Dim bv As BaseValidator
For Each bv In Page.Validators
bv.Enabled = bToggle
Next
End Sub

'Child changed event..
Dim cv As CustomValidator
ToggleValidators(False)

cv = mytable.FindControl(ID + "_validator")
If Not cv Is Nothing Then cv.Enabled = True

Page.Validate()
If Page.IsValid Then
' changed event handling for control ID.
End If

ToggleValidators(True)
'-----------------------------------------------

Hope someone can spot what's wrong with this.

Thanks
 
G

Guest

Adrian,

I'd try in addtion to turning on the validator to send an error message to
the client. Add a simple label to contain the error message. Cool technique
I'm going to try it I have so much trouble with validators not letting my
code run that I hardly ever use them.

Please do me a favor and vote for one of my entries at.
http://blog.mix06.com/contest/gallery/default.aspx
"The Hand is Quicker Than The Eye" or "Software"

Thank you and Good Luck
DWS
 
S

Steven Cheng[MSFT]

Hi Adrian,

As for the ASP.NET web pages, are you developing through ASP.NET 1.1 or
ASP.NET 2.0? As for ASP.NET 2.0, we can use the ValiationGroup to separate
different part of control on the pages to be validated through different
set of validators (and be triggered by different submit buttons). Therefore
I think you can configure those validtors you do not want to be validting
when post back(but let your own code to perform the valiation) to be a
different valiationgroup name. In addition, for the ASP.NET valiation
control, we can call the "Validate" method on an individual validator
instead of call validate method of the entire page, and check the IsValid
property on the validator control. I think this'll make the task much
simpler? for example:


protected void btnPost_Click(object sender, EventArgs e)
{
Response.Write("<br/>Normal Postback...");

Response.Write("<br/>RequiredFieldValidator3.IsValid: " +
RequiredFieldValidator3.IsValid);

this.RequiredFieldValidator3.Validate();

Response.Write("<br/>RequiredFieldValidator3.IsValid: " +
RequiredFieldValidator3.IsValid);
}


Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Adrian Parker

Never Mind, the validation technique works fine on a test screen, just not on the page I'm trying to use it on.
 
S

Steven Cheng[MSFT]

Hi Adrian,

I've also tried the approch in an ASP.NET 1.1 application on myside and it
works. We can just set the validator's "Enabled" to false initially. And
only reenable it at runtime(in postback event) when we want to perform the
validation. For example:


private void btnThree_Click(object sender, System.EventArgs e)
{

rfvThree.Enabled = true;


Response.Write("<br/>rfvThree.IsValid: " + rfvThree.IsValid);

rfvThree.Validate();

Response.Write("<br/>rfvThree.IsValid: " + rfvThree.IsValid);

rfvThree.Enabled = false;

}

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Adrian Parker

Hi Steven,

Excellent, that works well. In addition, I just needed to enable all the validators on the page at the top of any form submit events
before calling the page.validate() there.

Many Thanks
 

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

Top