Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

required field validator on checkboxlist

0 views
Skip to first unread message

Shannon Calma

unread,
Feb 11, 2002, 4:53:04 PM2/11/02
to
Frank,
This workaround required a little bit of "kludgery" but it does work:
* Add a checkboxlist to your form with the appropriate number of ListItem
objects
* Add a textbox to your form; set its visible property to false and its
default value to -1
* Add a RangeValidator to your form; set its ControlToValidate property to
the name of the textbox, the MinimumValue property to 0, the MaximumValue
property to (the number of checkboxes in the list -1) and Type property to
Integer
* Put in the code behind a method that handles the
CheckBoxList.SelectedIndexChanged event (just double click the control in
design mode and the stub will appear in the code-behind). Add to the
method: checkedTextBox.Text = CheckBoxList.SelectedIndex.ToString() '
i.e. set the Text property of the invisible textbox to the value of the
SelectedIndex of the CheckBoxList

What this will do is: If the user checks a checkbox in the CheckBoxList
the textbox will hold the value of the SelectedIndex (or if multiple are
selected, the smallest index of all the checked checkboxes). As long as the
value of the SelectedIndex property of the CheckBoxList is > -1 then you
want the form to submit. If the value of the SelectedIndex property = -1
then none of the checkboxes are checked. The RangeValidator will ensure
that the value in the textbox (which is the value of the SelectedIndex of
the CheckBoxList) is between 0 and the number of checkboxes.

I'm not sure if there is an easier/simpler way to achieve what you want, but
this will work.

Let me know if this doesn't make sense.

Shannon


--
Shannon Calma, MCSD
Clarity Consulting
http://www.claritycon.com
"FG3" <fran...@yahoo.com> wrote in message
news:23c601c1b32e$e2514910$39ef2ecf@TKMSFTNGXA08...
> I have a data-bound checkboxlist on a form and i want to
> make the checking of at least one checkbox required.
> apparently the <asp:requiredfieldvalidator> does not work
> with checkboxlists (although it does with
> radiobuttonlists).
>
> any ideas?
>
> Frank


Scott M.

unread,
Feb 11, 2002, 8:22:33 PM2/11/02
to
You can write your own validation scripts using a custom validator and
inserting a client script AND a server side validator as well.

See this for specifics:

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwebuiwebcontrolscustomvali
datorclasstopic.htm

0 new messages