custom validator won't validate

Last post 05-16-2008 12:53 PM by PLBlum. 4 replies.

Sort Posts:

  • custom validator won't validate

    05-16-2008, 12:26 AM
    • Participant
      843 point Participant
    • NoBullMan
    • Member since 06-28-2006, 5:27 PM
    • Virginia
    • Posts 551

    Hi.

    I am trying to validate a textbox using custom validator. If a specific radio button is checked and the textbox is empty, I want to display error. I used a custom validator with control-to-validate set to the textbox. In the server function, I check if radio button is checked and textbox is blank, set args.isvalid to false. However, when I trace it, the function is never hit. I have checked the properties and they are set properly (causes validation ,,,)

    Any idea what might be wrong?

    <asp:TextBox runat="server" ID="tbCoreFile" ReadOnly="true" Text="" />   (textbox gets populated dynamically)

    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Error ... Error" ControlToValidate="tbCoreFile"  ValidationGroup="vgGeneral" Display="Dynamic" Text="*"  OnServerValidate="CustomValidator1_ServerValidate" />

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) {

    if ((rbl.SelectedIndex == 0) && (args.Value.ToString().Trim() == string.Empty))

    args.IsValid = false;

    else

    args.IsValid = true;

    }

     

    Thanks,

  • Re: custom validator won't validate

    05-16-2008, 1:23 AM

    Hi,

    You have to call this.CustomValidator1.Validate() to fire the event.


    Cheers,

    Robin

    Robin Kedia
  • Re: custom validator won't validate

    05-16-2008, 2:22 AM
    • Participant
      843 point Participant
    • NoBullMan
    • Member since 06-28-2006, 5:27 PM
    • Virginia
    • Posts 551

    Thanks for the reply Robin. I had already done that and it didn't work. When I trace it, after clicking the button the is supposed to trigger the validation, it stops at this.CustomValidator1.Validate(), I try to step into it but it skips it and goes to the next line. The button that is clicked does fire up other validations, it is just this one validation that doesn't get fired.

  • Re: custom validator won't validate

    05-16-2008, 10:34 AM
    • Participant
      843 point Participant
    • NoBullMan
    • Member since 06-28-2006, 5:27 PM
    • Virginia
    • Posts 551

    Anyone?

  • Re: custom validator won't validate

    05-16-2008, 12:53 PM
    Answer
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 9:20 AM
    • Boston, MA
    • Posts 5,343
    • TrustedFriends-MVPs

    1. Set the ValidateEmptyText property to true on the CustomValidator. Otherwise, it never evaluates a blank textbox.

    2. Make sure your button's Click event tests Page.IsValid is true before using or saving the data.

    3. Make sure the button's validation group name matches the group name on the Validator.

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
Page 1 of 1 (5 items)