Using RequiredFieldvalidator with CheckBoxList

Last post 02-12-2008 3:01 PM by dineshsbisht. 10 replies.

Sort Posts:

  • Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 1:52 PM
    • Member
      216 point Member
    • robnot
    • Member since 02-20-2007, 11:50 PM
    • Posts 99

    RequireFieldValidators are easy enough to use with RadioButtonLists and DropDownLists, but not with CheckBoxLists.  What gives? Is there a way to use a RequiredFieldValidator with a CheckBoxList or an easy way to mimic the same functionality?

      thanks,

         Rob
     

  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 2:50 PM
    • Participant
      888 point Participant
    • KelseyThornton
    • Member since 07-15-2007, 12:01 PM
    • Breda, The Netherlands
    • Posts 229

    The problem with checkboxes is that, unlike the dropdownlist and radiobuttonlist, it is perfectly valid to not have any checkboxes selected (checked)...

    Kelsey Thornton
    (In the Netherlands)

    Don't forget - Mark the post which answered your question with "Answer", then that user will get some kudos, and your post will be marked as "Answered" for future readers!

    (VB code examples preferred)
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 3:13 PM
    • Member
      216 point Member
    • robnot
    • Member since 02-20-2007, 11:50 PM
    • Posts 99
    Is this an assumption in ASP.NET that means I'll have to use custom validation or is there some way to get a RequiredFieldValidator to do the job?
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 3:15 PM
    • Member
      154 point Member
    • mathijs_bibito
    • Member since 11-28-2007, 12:43 PM
    • Duiven, Netherlands
    • Posts 53

    Yes there is a way to validate this, just have to determine your own business rules for validation.


    The button or whatever that submits the values and must validate the values must have set the attribute CausesValidation  = true..

    The validation rules can be describe in the Validate() method of the page. (in your case, check if a checkbox is checked).

    In the OnClick event of the button you can call the Validate() method, and check if Page.IsValid == true.

    Hope this will give you an idea, there are thousands of good examples on the net and msdn.


     

    Mathijs - ASP.NET developer for Bibito project.
    http://www.bibito.nl
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 3:33 PM
    • Member
      216 point Member
    • robnot
    • Member since 02-20-2007, 11:50 PM
    • Posts 99
    Does all that happen on the client side before postback or is it handled by the server?
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 3:48 PM
    • Participant
      888 point Participant
    • KelseyThornton
    • Member since 07-15-2007, 12:01 PM
    • Breda, The Netherlands
    • Posts 229

    On modern browsers this should all be happening client-side

    There are also a couple of checkbox extenders in the AJAX toolkit which could be useful to you

    See http://www.asp.net/learn/ajax-videos/video-94.aspx and http://www.asp.net/learn/ajax-videos/video-132.aspx

    hope this helps!

    Kelsey Thornton
    (In the Netherlands)

    Don't forget - Mark the post which answered your question with "Answer", then that user will get some kudos, and your post will be marked as "Answered" for future readers!

    (VB code examples preferred)
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 3:49 PM
    • Member
      154 point Member
    • mathijs_bibito
    • Member since 11-28-2007, 12:43 PM
    • Duiven, Netherlands
    • Posts 53

    The validation happenes on the server side (runat="server").

    The client side can use AJAX/Java/HTML etc scripts (which were generated on the server)to generate the clientside output (server delivers html/java which updates in specific area in the page, and not the whole page's content), like divs that are set visible or alerts or whatever.

    I think it is a very nice way to validate a page. As another member posted, it is valid to have no checkboxes checked. So you have to implement that rule yourself. Which is actually pretty easy to do...

    Mathijs - ASP.NET developer for Bibito project.
    http://www.bibito.nl
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 5:03 PM
    Answer
    • Member
      231 point Member
    • dineshsbisht
    • Member since 02-06-2008, 4:55 PM
    • Tucson, AZ, USA
    • Posts 87

    if you use custom validator and leave the controltovalidate property empty, the checkbox will be validated at clientside whenever the page is trying to post back....e.g.. a button click etc

    --
    Regards,
    Dinesh Bisht

    http://www.mystudystuff.com/
    Place for old books, used books, study material, study notes etc. in India.
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 5:54 PM
    • Member
      216 point Member
    • robnot
    • Member since 02-20-2007, 11:50 PM
    • Posts 99
    Does that mean that I can just put a CustomValidator on the page without specifying a control and it'll still trigger client-side code before any postback to the server? That would be amazingly useful in so many ways.
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-11-2008, 6:00 PM
    • Member
      231 point Member
    • dineshsbisht
    • Member since 02-06-2008, 4:55 PM
    • Tucson, AZ, USA
    • Posts 87

    Yes you are right. In this case the custom validator will be page level and will be fired whenever you submit. Let me tell you what is the benefit of having ControlTovalidate, it shows you error mark when the value of the control (set as controltovalidate of the validator)  is invalid and you tab out from that control. But when your controltovalidte is empty in case of custom validator you will have to wait for the errormark until you submit you submit your page (click on submit).

    --
    Regards,
    Dinesh Bisht

    http://www.mystudystuff.com/
    Place for old books, used books, study material, study notes etc. in India.
  • Re: Using RequiredFieldvalidator with CheckBoxList

    02-12-2008, 3:01 PM
    • Member
      231 point Member
    • dineshsbisht
    • Member since 02-06-2008, 4:55 PM
    • Tucson, AZ, USA
    • Posts 87

    Did this solve your problem?. If yes mark this as answered Wink

    --
    Regards,
    Dinesh Bisht

    http://www.mystudystuff.com/
    Place for old books, used books, study material, study notes etc. in India.
Page 1 of 1 (11 items)