By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

CompareValidator firing in Page_Load, validating empty control

P: n/a
I have a textbox (StartDateTextBox) in a UserControl on my page, that
is supposed to have a date entered into it. I have a
RequiredFieldValidator that has its ControlToValidate property set to
the textbox. I also have a CompareValidator pointed to the textbox
with its Type="Date."

Despite all documentation to the contrary, I am getting an exception
when the page first loads that is thrown by the CompareValidator,
complaining that it cannot parse the empty string:

The value '' of the ValueToCompare property of 'StartDateValid' cannot
be converted to type 'Date'.

I am uncertain why this would be happening. I have composed the form
from a couple of user controls so I can reuse the same controls
separately on other pages. This UserControl has its set-up code
(determining which controls should be visible, the text of labels,
etc.) moved to a separate method which is called from Page_Load(). I
did this so the containing page can reset the control if it gets an
event from a different control.

Is the validation firing because the set-up code is not physically
within the Page_Load() method but is called from there? Even so, why
would the CompareValidator throw an exception instead of either
ignoring the empty value (which it is supposed to do) or setting its
IsValid property to false (which it should do if the value is in the
wrong format)?

Thanks - Eric
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Eric,
It sounds like you are trying to use the CompareValidator to validate a
control against a value. The CompareValidator is intended to compare two
controls to each other to ensure that the default property is the same. Ex:
compare two textboxes to ensure a user has entered a password twice and that
the passwords match.

See http://www.w3schools.com/aspnet/cont...evalidator.asp for some
examples of how to use the CompareValidator. Also check out the .Net
Framework SDK documentation for thorough documentation.

If you need to validate the value of a textbox to a value, then the
RangeValidator would be a good choice. Just set the beginning and end of
the range to be the same value.

If all you care about is that the value can be successfully parsed into a
DateTime object, then you'll have to perform a postback and try parsing the
value. You can do this with a CustomValidator and just write the server
side validation method. See
http://www.w3schools.com/aspnet/cont...mvalidator.asp

Best regards,
Jeffrey Palermo

"Eric Maia" <er*******@metrokc.gov> wrote in message
news:e7**************************@posting.google.c om...
I have a textbox (StartDateTextBox) in a UserControl on my page, that
is supposed to have a date entered into it. I have a
RequiredFieldValidator that has its ControlToValidate property set to
the textbox. I also have a CompareValidator pointed to the textbox
with its Type="Date."

Despite all documentation to the contrary, I am getting an exception
when the page first loads that is thrown by the CompareValidator,
complaining that it cannot parse the empty string:

The value '' of the ValueToCompare property of 'StartDateValid' cannot
be converted to type 'Date'.

I am uncertain why this would be happening. I have composed the form
from a couple of user controls so I can reuse the same controls
separately on other pages. This UserControl has its set-up code
(determining which controls should be visible, the text of labels,
etc.) moved to a separate method which is called from Page_Load(). I
did this so the containing page can reset the control if it gets an
event from a different control.

Is the validation firing because the set-up code is not physically
within the Page_Load() method but is called from there? Even so, why
would the CompareValidator throw an exception instead of either
ignoring the empty value (which it is supposed to do) or setting its
IsValid property to false (which it should do if the value is in the
wrong format)?

Thanks - Eric

Nov 18 '05 #2

P: n/a
Set the Operator property to DataTypeCheck. Until you do, it will look in
the ValueToCompare property.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Eric Maia" <er*******@metrokc.gov> wrote in message
news:e7**************************@posting.google.c om...
I have a textbox (StartDateTextBox) in a UserControl on my page, that
is supposed to have a date entered into it. I have a
RequiredFieldValidator that has its ControlToValidate property set to
the textbox. I also have a CompareValidator pointed to the textbox
with its Type="Date."

Despite all documentation to the contrary, I am getting an exception
when the page first loads that is thrown by the CompareValidator,
complaining that it cannot parse the empty string:

The value '' of the ValueToCompare property of 'StartDateValid' cannot
be converted to type 'Date'.

I am uncertain why this would be happening. I have composed the form
from a couple of user controls so I can reuse the same controls
separately on other pages. This UserControl has its set-up code
(determining which controls should be visible, the text of labels,
etc.) moved to a separate method which is called from Page_Load(). I
did this so the containing page can reset the control if it gets an
event from a different control.

Is the validation firing because the set-up code is not physically
within the Page_Load() method but is called from there? Even so, why
would the CompareValidator throw an exception instead of either
ignoring the empty value (which it is supposed to do) or setting its
IsValid property to false (which it should do if the value is in the
wrong format)?

Thanks - Eric

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.