CustomValidator OnServerValidate Not Firing RRS feed

  • Question

  • User46949095 posted

    I have a registration wizard that has multiple steps. On one step, I have a ListBox control that needs to have at least one item in it's ListItems collection before you can continue to the next step.

    Here is the custom validator in the wizard step:

    <asp:CustomValidator ID="cvFavorites" runat="server" ControlToValidate="lbFavorites" OnServerValidate="cvFavorites_Validate" ValidationGroup="positionInfo" ErrorMessage="Please choose at least one position" Display="Static" Text=" *" EnableClientScript="false" ForeColor="Red" Font-Size="9px" Font-Names="Tahoma, Arial"> * Please select at least one position</asp:CustomValidator>

    ... Then, in my compiled library that implements the page, I have this:

    protected void cvFavorites_Validate(object source, ServerValidateEventArgs value)


       bool valid = false;

       if (lbFavorites.Items.Count > 0)

          valid = true;


       value.IsValid = valid;


    I've put a break point on the first line of the event handler method, highlighted in red, and I can't figure out why it never fires.

    protected void wizRegistration_NextClick(object sender, WizardNavigationEventArgs e)


    bool dontContinue = false;

    if (wizRegistration.ActiveStep == stepSelectSeason)


    else if (wizRegistration.ActiveStep == stepPositions)




       if (!Page.IsValid)


          dontContinue = true;



    e.Cancel = dontContinue;


    I've discovered that the event never fires, and I can't understand what I'm doing wrong. Please advise!

    Saturday, February 11, 2006 2:06 PM

All replies

  • User-1513591455 posted

    There are three ways to validate a page

    Add a button that is designed to force a page validation (e.g. link button) - ensure that the validate attribute is set to true (it's the default, so an absence is OK).  Add a button anywhere on your page, run your page, then click it (you don't need an event handler for it.  Just clicking it is enough) - your event should fire.  Note that some controls might cause a post-back (e.g. CheckBox with auto-postback), but not validation.

    Call the Validate method - put a breakpoint in your code to check this is called in your example

    Iterate the IValidator collection of a web page manually

    Saturday, February 11, 2006 7:00 PM
  • User910519897 posted

    Hi Corren,

    i had this exact problem last week.

    The problem is that the validator does not fire on a listbox unless some listitem is selected. If you have no items in your lsitbox then no item can be selected so the validator does not fire.

    The solution is to use a hidden control to cause the validation to fire:

    I got around this problem by adding a hidden text field with some rubbish text in it.. and set its ID ="ValidationFudge1" then in the custom validator I set its controlToValidate to be = ValidationFudge1 . So the validator WILL fire because the text field DOES have something in it.

    But, in then the code of the custom validator i went and checked the itemcount of the listbox instead of looking at the textbox.

    I can post some code to expplain this further for you if you need it.

    I was doing this as part of a 'mover' control... i have two listboxes.. left one has the available selections, right one will contain the selected items for the user.. then I have two buttons between the listboxes .. a move right and move left button... clicking on the MoveRight button takes the selected item from the left listbox and moves it to the right listbox.. (note both buttons have CausesValidation set to false).. then when the user clicks on the Next button in the wizard the validation fires (as a result of the ValidationFudge1 control being there..) so there is no need to explicitly call the validation in the NextClick event...



    Tuesday, March 20, 2007 8:03 AM
  • User845932231 posted

    Make sure your submit button has the same validation group... as we have with the validator


    Sunday, February 21, 2010 6:30 AM