Server Side CustomValidator not working

Last post 05-09-2008 4:59 PM by akhil_csit. 7 replies.

Sort Posts:

  • Server Side CustomValidator not working

    02-06-2008, 6:01 PM
    • Loading...
    • Varnell
    • Joined on 01-16-2008, 4:33 PM
    • Posts 12

    When I use the CustomValidate for server side validation, it doesn't seem to execute the server code. All my server code does (at the moment) is set args.IsValid = false; But everytime I run it, if I put anything in the textbox I'm validating, it passes. I put a break in the server code and it doesn't break. I've set the "ControlToValidate" to the right text box.

    What am I missing? What other properties need setting for it to work?

     I'm new to ASP and VS2005 so I'm probably missing something.

    Thanks

  • Re: Server Side CustomValidator not working

    02-06-2008, 9:49 PM

    If you have a button that should trigger validation try putting this code in the top of the event handler (for the button) 

    Page.Validate(); 

    if (!Page.isValid) return;

     Technically in most instances you don't need the Page.Validate() method.  I've observed that sometimes UpdatePanels will result in validation not firing as expected and Page.Validate() seems to correct it.  Also,  in ASP.NET 1.1 there was an issue with validation not firing with FireFox and some other browsers.  That is when I developed the habit of including the Page.isValid check.

     Hope this helps.

    Matt
    Chief Architect
    Software Engineer
    Smooth Fusion, Inc.
    http://www.smoothfusion.com
  • Re: Server Side CustomValidator not working

    02-07-2008, 10:14 AM
    • Loading...
    • Varnell
    • Joined on 01-16-2008, 4:33 PM
    • Posts 12

    It appears there is a hierarchy in the Validation scheme. I found that the "RequiredFieldValidation" fields are evaluated and marked appropriately. If any fail the validation, it doesn't do the other Validations. Once all the RequiredFieldValidations are done then it moves on to the other. Since I only use (at this time) required and customValidation I only know it doesn't do the custom until the required are complete.

     Thanks for the help, you got me off dead center and moving.

  • Re: Server Side CustomValidator not working

    02-07-2008, 12:43 PM

     While you are using custom validator then you have to disable client side validation then after it will work.

    if this is your answer then please mark as answer, thank you.

  • Re: Server Side CustomValidator not working

    02-07-2008, 1:25 PM
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,126
    • TrustedFriends-MVPs

    There are several reasons it may not fire:

    1. The ServerValidate event is not attached to your event handler method.

    2. The control is Enabled=false or Visible=false

    3. The control is contained in another control whose Visible property is false.

    4. The validation group feature is being used. The validation group name on the button differs from the value in the Validator. (This is a common source of the problem)

    5. If the textbox is blank, it will not fire unless ValidateEmptyText is true.

    6. The button submitting the page has CausesValidation=false. This will prevent all validators from being evaluated on the server side unless you explicitly call Page.Validate()

    --- 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
  • Re: Server Side CustomValidator not working

    02-07-2008, 1:27 PM
    Answer
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,126
    • TrustedFriends-MVPs

    Varnell:
    It appears there is a hierarchy in the Validation scheme. I found that the "RequiredFieldValidation" fields are evaluated and marked appropriately. If any fail the validation, it doesn't do the other Validations. Once all the RequiredFieldValidations are done then it moves on to the other. Since I only use (at this time) required and customValidation I only know it doesn't do the custom until the required are complete.

     

    I would like to clear up this misconception. Validators are evaluated in the order they appear in the Page.Validators collection. In fact, that collection is based on the order they appear in the control tree of the page. You may have had your RequiredFieldValidators earlier on the page than others.

    --- 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
  • Re: Server Side CustomValidator not working

    02-07-2008, 1:29 PM
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,126
    • TrustedFriends-MVPs

    jaymehta_26:
    While you are using custom validator then you have to disable client side validation then after it will work.

     

    I would like to clear up this misconception too. CustomValidators will operate fine whether client-side validation is supported or not. You can have EnableClientScript=true and the validator will have no effect on the client-side unless you assign the ClientValidationFunction property. The key here is that you should always setup server side validation. Its already been mentioned. Make sure Page.Validate() runs (which happens when the button submits the page so long as its CausesValidation property is true). Your responsibility is to test Page.IsValid is true in your button's Click event handler before trying to save or use the data from the page.

    --- 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
  • Re: Server Side CustomValidator not working

    05-09-2008, 4:59 PM
    • Loading...
    • akhil_csit
    • Joined on 05-09-2008, 4:58 PM
    • Posts 1

    Thanx a lot. It worked for me right away.

Page 1 of 1 (8 items)