|
| 1 | +--- |
| 2 | +title: Make Items Exclusive and Randomize Selections in RadCheckBoxList |
| 3 | +description: Learn how to mark certain RadCheckBoxList items as exclusive, enabling a unique selection behavior, select all non-exclusive items, and randomize the list order. |
| 4 | +type: how-to |
| 5 | +page_title: Make Items Exclusive and Randomize Selections in RadCheckBoxList |
| 6 | +slug: checkboxlist-exclusive-selection-randomization |
| 7 | +tags: radcheckboxlist, asp.net ajax, exclusive selection, randomize list, select all, buttonlistitem |
| 8 | +res_type: kb |
| 9 | +ticketid: 1669462 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadCheckBoxList for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>All</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | +I'm working with a RadCheckBoxList with multi-choice enabled and need to implement several behaviors. First, I want to mark one or more choices as "exclusive" - selecting one of these clears all other selections. Secondly, I need a way to select all values except the exclusive ones. Lastly, I'm looking to randomize the list of items. This KB article also answers the following questions: |
| 29 | + |
| 30 | +- How can I make selections in RadCheckBoxList mutually exclusive? |
| 31 | +- What is the approach to select all items except certain ones in RadCheckBoxList? |
| 32 | +- How can I randomize the items in RadCheckBoxList? |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +To customize the selection behavior and list order in RadCheckBoxList, follow these steps: |
| 37 | + |
| 38 | +### Marking Items as Exclusive |
| 39 | + |
| 40 | +Handle the `OnSelectedIndexChanged` event. When an exclusive item is selected, clear all other selections. Use a list to manage exclusive items based on your data source. |
| 41 | + |
| 42 | +````C# |
| 43 | +protected void RadCheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) |
| 44 | +{ |
| 45 | + List<string> exclusiveItems = new List<string> { "1", "7", "10" }; // Example exclusive items |
| 46 | +
|
| 47 | + foreach (ButtonListItem item in RadCheckBoxList1.Items) |
| 48 | + { |
| 49 | + if (exclusiveItems.Contains(item.Value) && item.Selected) |
| 50 | + { |
| 51 | + foreach (ButtonListItem otherItem in RadCheckBoxList1.Items) |
| 52 | + { |
| 53 | + if (!exclusiveItems.Contains(otherItem.Value)) |
| 54 | + { |
| 55 | + otherItem.Selected = false; |
| 56 | + } |
| 57 | + } |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +```` |
| 63 | + |
| 64 | +### Select All Non-Exclusive Items |
| 65 | + |
| 66 | +Add a button to select all items except the exclusive ones. Refer to the same list of exclusive items to identify which items to select. |
| 67 | + |
| 68 | +````C# |
| 69 | +protected void SelectAllButton_Click(object sender, EventArgs e) |
| 70 | +{ |
| 71 | + List<string> exclusiveItems = new List<string> { "1", "7", "10" }; // Example exclusive items |
| 72 | +
|
| 73 | + foreach (ButtonListItem item in RadCheckBoxList1.Items) |
| 74 | + { |
| 75 | + if (!exclusiveItems.Contains(item.Value)) |
| 76 | + { |
| 77 | + item.Selected = true; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +```` |
| 82 | + |
| 83 | +### Randomize the List of Items |
| 84 | + |
| 85 | +Shuffle the list items programmatically and rebind them to the RadCheckBoxList. |
| 86 | + |
| 87 | +````C# |
| 88 | +protected void RandomizeButton_Click(object sender, EventArgs e) |
| 89 | +{ |
| 90 | + List<ButtonListItem> items = RadCheckBoxList1.Items.Cast<ButtonListItem>().ToList(); |
| 91 | + List<ButtonListItem> shuffledItems = items.OrderBy(item => Guid.NewGuid()).ToList(); |
| 92 | + |
| 93 | + RadCheckBoxList1.Items.Clear(); |
| 94 | + |
| 95 | + foreach (ButtonListItem item in shuffledItems) |
| 96 | + { |
| 97 | + RadCheckBoxList1.Items.Add(item); |
| 98 | + } |
| 99 | +} |
| 100 | +```` |
| 101 | + |
| 102 | +### Full code snippet |
| 103 | + |
| 104 | +````ASP.NET |
| 105 | +<telerik:RadCheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="RadCheckBoxList1_SelectedIndexChanged" > |
| 106 | + <Items> |
| 107 | + <telerik:ButtonListItem Text="Item1" Value="1" /> |
| 108 | + <telerik:ButtonListItem Text="Item2" Value="2" /> |
| 109 | + <telerik:ButtonListItem Text="Item3" Value="3" /> |
| 110 | + <telerik:ButtonListItem Text="Item4" Value="4" /> |
| 111 | + <telerik:ButtonListItem Text="Item5" Value="5" /> |
| 112 | + <telerik:ButtonListItem Text="Item6" Value="6" /> |
| 113 | + </Items> |
| 114 | +</telerik:RadCheckBoxList> |
| 115 | +
|
| 116 | +<telerik:RadButton runat="server" ID="RadButton2" Text="Select non-exclusive values" AutoPostBack="true" OnClick="RadButton1_Click" /> |
| 117 | +<telerik:RadButton runat="server" ID="RadButton1" Text="Randomize" AutoPostBack="true" OnClick="RandomizeButton_Click" /> |
| 118 | +```` |
| 119 | + |
| 120 | +````C# |
| 121 | +List<string> exclusiveItems = new List<string> { "1", "3", "5" }; // Example exclusive items |
| 122 | +
|
| 123 | +protected void RadCheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) |
| 124 | +{ |
| 125 | + foreach (ButtonListItem item in CheckBoxList1.Items) |
| 126 | + { |
| 127 | + if (exclusiveItems.Contains(item.Value) && item.Selected) |
| 128 | + { |
| 129 | + foreach (ButtonListItem otherItem in CheckBoxList1.Items) |
| 130 | + { |
| 131 | + if (!exclusiveItems.Contains(otherItem.Value)) |
| 132 | + { |
| 133 | + otherItem.Selected = false; |
| 134 | + } |
| 135 | + } |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +protected void RadButton1_Click(object sender, EventArgs e) |
| 142 | +{ |
| 143 | + foreach (ButtonListItem item in CheckBoxList1.Items) |
| 144 | + { |
| 145 | + if (!exclusiveItems.Contains(item.Value)) |
| 146 | + { |
| 147 | + item.Selected = true; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +protected void RandomizeButton_Click(object sender, EventArgs e) |
| 153 | +{ |
| 154 | + ButtonListItemCollection itemsList = CheckBoxList1.Items; |
| 155 | + |
| 156 | + List<ButtonListItem> items = CheckBoxList1.Items.Cast<ButtonListItem>().ToList(); |
| 157 | + List<ButtonListItem> shuffledItems = items.OrderBy(item => Guid.NewGuid()).ToList(); |
| 158 | + |
| 159 | + CheckBoxList1.Items.Clear(); |
| 160 | + |
| 161 | + foreach (ButtonListItem item in shuffledItems) |
| 162 | + { |
| 163 | + CheckBoxList1.Items.Add(item); |
| 164 | + } |
| 165 | +} |
| 166 | +```` |
| 167 | + |
| 168 | +These steps provide a foundation for implementing exclusive selection, a select-all function respecting exclusivity, and randomizing RadCheckBoxList items. Adjust the code samples to fit your specific requirements and data source. |
| 169 | + |
| 170 | +## See Also |
| 171 | + |
| 172 | +- [RadCheckBoxList Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/checkboxlist/overview) |
| 173 | +- [RadCheckBoxList Client-Side Programming](https://docs.telerik.com/devtools/aspnet-ajax/controls/checkboxlist/client-side-programming/overview) |
0 commit comments