Custom CheckBoxList in ASP.NET
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 program开发者_JAVA百科matically) 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.
An easier way to solve this problem is to use the standard CheckBoxList, but create a special server validator control specifically for the CheckBoxList. The following article shows you how to go about this - Creating Validator Controls for the CheckBox and CheckBoxList.
精彩评论