CompareValidator firing in Page_Load, validating empty control

Discussion in 'ASP .Net' started by Eric Maia, Nov 17, 2004.

  1. Eric Maia

    Eric Maia Guest

    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
     
    Eric Maia, Nov 17, 2004
    #1
    1. Advertising

  2. 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/control_comparevalidator.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/control_customvalidator.asp

    Best regards,
    Jeffrey Palermo

    "Eric Maia" <> wrote in message
    news:...
    > 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
     
    Jeffrey Palermo [MCP], Nov 17, 2004
    #2
    1. Advertising

  3. Eric Maia

    Peter Blum Guest

    Set the Operator property to DataTypeCheck. Until you do, it will look in
    the ValueToCompare property.

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

    "Eric Maia" <> wrote in message
    news:...
    >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
     
    Peter Blum, Nov 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C K
    Replies:
    2
    Views:
    3,769
  2. Jorge EA
    Replies:
    1
    Views:
    584
    Peter Blum
    Sep 4, 2004
  3. MattB

    CompareValidator not firing

    MattB, Apr 5, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,728
    MattB
    Apr 5, 2005
  4. Ahmet Gunes
    Replies:
    1
    Views:
    1,702
    Norman Yuan
    Jun 19, 2005
  5. Jason
    Replies:
    1
    Views:
    608
    Jason
    Oct 23, 2006
Loading...

Share This Page