473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

CompareValidator firing in Page_Load, validating empty control

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
2 3168
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: buran | last post by:
Dear ASP.NET Programmers, I am using a CompareValidator control to check the values entered into a texbox. The textbox is for currencies, so the property Operator is set to DataTypeCheck and the...
3
by: Mike | last post by:
Hi, I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-( namespace...
0
by: RSB | last post by:
Hi Everyone, I am using comparevalidator with Operator DateTypeCheck. And recently i have chenged my devlopment machine.. So on my First Machine the validatod was validating the Date with the...
4
by: MattB | last post by:
I have a page that has gone through a lot of editing. It has many text box and validator controls, and one compare validator just isn't working. I have the control to validate set, and control to...
4
by: Seraph | last post by:
Again, I'm rather new here, so if I fail to follow any etiquette, please forgive me and let me know what I've done wrong, but I think this might interest quite a few people. One of my colleaques...
28
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
1
by: R.A.M. | last post by:
Hello, (Sorry for my English...) Could you help me please? I have a problem with CompareValidator control which makes the load of my page infinitive. Here's the code: Date of birth:...
1
by: dnnddane | last post by:
Hi, I guess you all already know about the comparevalidator when the value of input control(ControlToValidate) is empty/null, it causes the validation success. I know we can use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.