|
| 1 | +--- |
| 2 | +title: Select Multiple Items in Code-Behind with CheckBoxes |
| 3 | +description: Learn how to programmatically select multiple items in a RadComboBox when CheckBoxes are enabled, using the Page_Load event in ASP.NET AJAX. |
| 4 | +type: how-to |
| 5 | +page_title: Select Multiple Items in Code-Behind with CheckBoxes |
| 6 | +slug: combobox-select-multiple-items-in-code-behind-with-checkboxes |
| 7 | +tags: radcombobox, asp.net ajax, checkboxes, selection, programmatically |
| 8 | +res_type: kb |
| 9 | +ticketid: 1670621 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadComboBox 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 | + |
| 29 | +When using the [RadComboBox](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview) with `CheckBoxes="True"`, I need to allow the selection of multiple values. How can I programmatically set some values as selected on the Page-Load of the page? This KB article also answers the following questions: |
| 30 | + |
| 31 | +- How to check multiple items in RadComboBox programmatically? |
| 32 | +- How to set selected values in RadComboBox with CheckBoxes on page load? |
| 33 | +- How to highlight selected items in RadComboBox with CheckBoxes? |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +To achieve the selection of multiple values in a RadComboBox with checkboxes enabled, use the `Page_Load` event to iterate over the items and set the `Checked` property of each `RadComboBoxItem` that you want to select to `true`. Here is a step-by-step guide to accomplish this: |
| 38 | + |
| 39 | +1. **Prepare a List of Values to Select**: Identify and prepare a list of the values that you want to be selected by default. |
| 40 | + |
| 41 | +2. **Iterate Over RadComboBox Items**: Loop through each item in the RadComboBox. |
| 42 | + |
| 43 | +3. **Check Items Based on Values**: For each item, check if its value is in the list of values to select. If so, set its `Checked` property to `true`. |
| 44 | + |
| 45 | +### Code Example |
| 46 | + |
| 47 | +````ASP.NET |
| 48 | +<telerik:RadComboBox ID="RadComboBox1" runat="server" RenderMode="Lightweight" OnClientLoad="onLoad" |
| 49 | + EmptyMessage="Select item" CheckBoxes="True" EnableCheckAllItemsCheckBox="True"> |
| 50 | + <Items> |
| 51 | + <telerik:RadComboBoxItem Text="Item 1" Value="Value1" /> |
| 52 | + <telerik:RadComboBoxItem Text="Item 2" Value="Value2" /> |
| 53 | + <telerik:RadComboBoxItem Text="Item 3" Value="Value3"/> |
| 54 | + <telerik:RadComboBoxItem Text="Item 4" Value="Value4"/> |
| 55 | + </Items> |
| 56 | +</telerik:RadComboBox> |
| 57 | +```` |
| 58 | + |
| 59 | +````C# |
| 60 | +protected void Page_Load(object sender, EventArgs e) |
| 61 | +{ |
| 62 | + if (!IsPostBack) |
| 63 | + { |
| 64 | + // List of values you want to select by default |
| 65 | + List<string> selectedValues = new List<string> { "Value1", "Value2", "Value3" }; |
| 66 | + |
| 67 | + // Iterate over the items in the RadComboBox |
| 68 | + foreach (RadComboBoxItem item in rcbCompagnie.Items) |
| 69 | + { |
| 70 | + // Check if the item's value is in the list of selected values |
| 71 | + if (selectedValues.Contains(item.Value)) |
| 72 | + { |
| 73 | + item.Checked = true; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +```` |
| 79 | + |
| 80 | +### Styling Selected Items |
| 81 | + |
| 82 | +To style the selected items, you can use the `OnClientLoad` event: |
| 83 | + |
| 84 | +````JavaScript |
| 85 | +function onLoad(sender, args) { |
| 86 | + let items = sender.get_items().toArray(); |
| 87 | + let checkedItems = items.filter((item) => item.get_checked()); |
| 88 | + |
| 89 | + checkedItems.forEach((item) => { |
| 90 | + item.get_element().style.backgroundColor = "red"; |
| 91 | + }) |
| 92 | +} |
| 93 | +```` |
| 94 | + |
| 95 | +## See Also |
| 96 | + |
| 97 | +- [RadComboBox Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview) |
| 98 | +- [Client-Side Programming of RadComboBox](https://www.telerik.com/products/aspnet-ajax/documentation/controls/combobox/client-side-programming/events/onclientload#onclientload) |
0 commit comments