Custom CheckBoxList in ASP.NET

Posted by Rick on Stack Overflow See other posts from Stack Overflow or by Rick
Published on 2010-04-09T15:57:11Z Indexed on 2010/04/10 4:23 UTC
Read the original article Hit count: 452

Since ASP.NET's CheckBoxList control does not allow itself to be validated with one of the standard validation controls (i.e., RequiredFieldValidator), I would like to create a UserControl that I can use in my project whenever I need a checkbox list that requires one or more boxes to be checked.

The standard CheckBoxList can be dragged onto a page, and then you can manually add <asp:ListItem> controls if you want. Is there any way I can create a UserControl that lets me manually (in the markup, not programmatically) insert ListItems from my page in a similar manner?

In other words, can I insert a UserControl onto a page, and then from the Designer view of the Page (i.e., not the designer view of the UserControl), can I manually add my ListItems like so:

<uc1:RequiredCheckBoxList>
   <asp:ListItem Text="A" value="B"></asp:ListItem>
   <asp:ListItem Text="X" value="Y"></asp:ListItem>
</uc1:RequiredCheckBoxList>

If a UserControl is not the appropriate choice for the end result I'm looking for, I'm open to other suggestions.

Please note that I am aware of the CustomValidator control (which is how I plan to validate within my UserControl). It's just a pain to write the same basic code each time I need one of these required checkbox lists, which is why I want to create a re-usable control.

© Stack Overflow or respective owner

Related posts about checkboxlist

Related posts about ASP.NET