Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

Validating ASP.NET CheckBox (and similar) controls.

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
20,238
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Jan 4, 2008

http://blog.dmbcllc.com There are controls in the .NET framework that can not be wired to the standard validation controls. The checkbox control is one example. You can't use the RequiredFieldValidator because it has a value. It's either true or false. And you can't provide a RegularExpressionValidator or one of the others because it is a boolean value.

So, if I want to make sure a check box is checked before the user continues, for example. And, I want to make sure that the error message shows up in the error summary control like every other error, how do I do that?

Category:

People & Blogs

Tags:

License:

Standard YouTube License

  • likes, 2 dislikes

Link to this comment:

Share to:

Uploader Comments (davidmbush)

  • I just cannot get this to work. I already have a lot of RequiredField validators on my page and they work fine, with the ValildationSummary displaying all of the relevant error messages in one place. But even though I've specified the same ValidationGroup for the CustomValidator the error isn't displayed there. In fact, it won't return an IsValid value of False no matter what I do. Even when my code in the ServerValidate event is simply 'args.IsValid = False'!

  • @phil2415 Your question was? I hear your frustration, but no one can help you unless you ask a specific question.

  • @davidmbush I apologise, my question was just 'what am I doing wrong'? All of these controls are within a Wizard control and all of the standard RequiredField validators trigger when I click the 'Next' button, but the CustomValidator just doesn't seem to trigger at the same time. I added a line to report the validity of the checkbox and this doesn't appear until after all of the other validators have been satisfied, and even then it reports 'false' but lets the wizard advance anyway.

  • @phil2415 I'm going to guess that you don't have an event handler in the wizard to handle the "Next" that will check the "IsValid" property of the page. The other validations are working because they are using javascript. This is a server side validation so it doesn't trigger until you get to the server. You need something to prevent the "Next" from happening if the validations fail.

  • @davidmbush I do appreciate your help. As per your tutorial I added a 'If IsValid Then' line into the code behind for the next button but the problem is that while this prevents the wizard advancing if any of my RequiredField validators are invalid the CustomValidator doesn't seem to affect the IsValid status of the page itself, since even though the CustomValidator is reporting as 'False' the wizard advances anyway.

  • @phil2415 from here I'd need to see code. Sorry, I can't be of more help on this limited medium

see all

All Comments (22)

Sign In or Sign Up now to post a comment!
  • It is useful but i want to know the checkbox when it is bind with listview..

  • good one

  • Thanks, this was helpful. I got it to work using a checkboxlist in a wizard.

    Follow the above example, set args.isvalid in the ServerValidateEventArgs event. Use a foreach loop to loop through ListItems li in checkboxlist1. Then use an if statement to check if li.Selected is true. If true set args.Isvalid to true.

  • @davidmbush Don't apologise, your help is very much appreciated. You put me on the right track and now I've figured it out. The page was 'invalid' but my code to prevent the wizard from advancing if this was the case was wrong. I've now got:

    If IsValid = False Then e.Cancel = True

    End If

    in the NextButtonClick event of the wizard. So now when I click the Next button the RequiredField validators kick in, then the page posts back and the CustomValidator does its thing.

    Thanks again!!

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more