Reputation: 568
Is there any way that I can wrap a checkboxlist in asp.net so that lets say if you have 20 checkboxes in your list, there are two columns of 10. Example:
Box Box
Box Box
Box Box
Box Box
Box Box
Box Box
Box Box
Box Box
My current code is just:
<asp:CheckBoxList ID="Numbers" runat="server" AutoPostBack = "true">
<asp:ListItem> 1 </asp:ListItem>
<asp:ListItem> 2 </asp:ListItem>
<asp:ListItem> 3 </asp:ListItem>
<asp:ListItem> 4 </asp:ListItem>
<asp:ListItem> 5 </asp:ListItem>
<asp:ListItem> 6 </asp:ListItem>
<asp:ListItem> 7 </asp:ListItem>
<asp:ListItem> 8 </asp:ListItem>
<asp:ListItem> 9 </asp:ListItem>
...ect
</asp:CheckBoxList>
I figure there must be some sort of asp markup or something to allow me to cleanup this list of checkboxes.
Upvotes: 3
Views: 3154
Reputation:
What you are looking for is the RepeatColumns
property and the RepeatDirection
property. Something like this:
<asp:CheckBoxList ID="Numbers" runat="server" AutoPostBack = "true"
RepeatColumns="2" RepeatDirection="Vertical">
<asp:ListItem> 1 </asp:ListItem>
<asp:ListItem> 2 </asp:ListItem>
<asp:ListItem> 3 </asp:ListItem>
<asp:ListItem> 4 </asp:ListItem>
<asp:ListItem> 5 </asp:ListItem>
<asp:ListItem> 6 </asp:ListItem>
<asp:ListItem> 7 </asp:ListItem>
<asp:ListItem> 8 </asp:ListItem>
<asp:ListItem> 9 </asp:ListItem>
...ect
</asp:CheckBoxList>
As per the below reference, you can also set this property programmatically in your code-behind utilizing the RepeatDirection
enum:
Numbers.RepeatDirection = RepeatDirection.Vertical;
Numbers.RepeatColumns = 2;
Please see this as a reference: How to: Set Layout in a CheckBoxList Web Server Control
Upvotes: 5
Reputation: 8206
Yes, use the RepeatColumns property. If you only want it to show that way after you have dynamically bound the control and it has more than ten items, just count the items in the Page_Load and set the RepeatColumns to 2 if the datasource contains more than 10 elements.
Here is the msdn documentation for more information.
Upvotes: 3