|
| 1 | +--- |
| 2 | +title: Adding a "Select All" Checkbox to RadMultiSelect dropdown header |
| 3 | +description: Learn how to add a "Select All" checkbox in the RadMultiSelect component header to easily select or deselect all items. |
| 4 | +type: how-to |
| 5 | +page_title: How to Implement a "Select All" Checkbox in RadMultiSelect for ASP.NET AJAX |
| 6 | +slug: how-to-add-select-all-checkbox-radmultiselect |
| 7 | +tags: radmultiselect, multiselect, select-all, header-template, asp.net ajax |
| 8 | +res_type: kb |
| 9 | +ticketid: 1673089 |
| 10 | +--- |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +I want to add a "Select All" checkbox in the MultiSelect component to allow users to select or deselect all items in the dropdown. Utilizing the headerTemplate configuration enables rendering a checkbox at the top of the dropdown list, facilitating the selection or deselection of all items through user interaction. |
| 15 | + |
| 16 | +This knowledge base article also answers the following questions: |
| 17 | +- How to implement a "Select All" feature in MultiSelect? |
| 18 | +- How to customize the header of the MultiSelect popup? |
| 19 | +- How to manage multiple selections in MultiSelect through a single checkbox? |
| 20 | + |
| 21 | +## Environment |
| 22 | + |
| 23 | +<table> |
| 24 | +<tbody> |
| 25 | +<tr> |
| 26 | +<td>Product</td> |
| 27 | +<td>RadAjax for ASP.NET AJAX</td> |
| 28 | +</tr> |
| 29 | +</tbody> |
| 30 | +</table> |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +To add a "Select All" checkbox in the RadMultiSelect component, utilize the `HeaderTemplate` configuration. This template renders a checkbox at the top of the dropdown list, allowing users to select or deselect all items with a single interaction. |
| 35 | + |
| 36 | +Here is how to implement the "Select All" checkbox feature in the `RadMultiSelect` component: |
| 37 | + |
| 38 | +1. Define the `RadMultiSelect` component with the required settings and data fields. Include the `HeaderTemplate` that contains the "Select All" checkbox. |
| 39 | + |
| 40 | +```ASPX |
| 41 | + <telerik:RadMultiSelect runat="server" Width="100%" ID="RadMultiSelect1" |
| 42 | + DataTextField="text" |
| 43 | + DataValueField="text" |
| 44 | + DropDownHeight="400" |
| 45 | + CssClass="customers-wrapper" |
| 46 | + Placeholder="Select attendees..."> |
| 47 | + <Items> |
| 48 | + <telerik:MultiSelectItem Text="Steven White"></telerik:MultiSelectItem> |
| 49 | + <telerik:MultiSelectItem Text="Nancy King"></telerik:MultiSelectItem> |
| 50 | + <telerik:MultiSelectItem Text="Nancy Davolio"></telerik:MultiSelectItem> |
| 51 | + <telerik:MultiSelectItem Text="Robert Davolio"></telerik:MultiSelectItem> |
| 52 | + <telerik:MultiSelectItem Text="Michael Leverling"></telerik:MultiSelectItem> |
| 53 | + <telerik:MultiSelectItem Text="Andrew Callahan"></telerik:MultiSelectItem> |
| 54 | + <telerik:MultiSelectItem Text="Michael Suyama"></telerik:MultiSelectItem> |
| 55 | + <telerik:MultiSelectItem Text="Anne King"></telerik:MultiSelectItem> |
| 56 | + <telerik:MultiSelectItem Text="Laura Peacock"></telerik:MultiSelectItem> |
| 57 | + <telerik:MultiSelectItem Text="Robert Fuller"></telerik:MultiSelectItem> |
| 58 | + </Items> |
| 59 | + <HeaderTemplate> |
| 60 | + <div style="padding:4px 8px"> |
| 61 | + <input type="checkbox" id="selectAll" /> Select All |
| 62 | + </div> |
| 63 | + </HeaderTemplate> |
| 64 | + <ClientEvents OnLoad="OnLoad" /> |
| 65 | + </telerik:RadMultiSelect> |
| 66 | +``` |
| 67 | + |
| 68 | +2. Implement the JavaScript functions to handle the "Select All" and "Deselect All" actions. Attach these functions to the "Select All" checkbox within the `OnLoad` client-side event. |
| 69 | + |
| 70 | +```javascript |
| 71 | +<script type="text/javascript"> |
| 72 | + function OnLoad(sender, args) { |
| 73 | + // Attach the Select All logic to the checkbox |
| 74 | + var selectAllCheckbox = document.getElementById("selectAll"); |
| 75 | + selectAllCheckbox.addEventListener("change", function () { |
| 76 | + var multiSelect = $find("<%= RadMultiSelect1.ClientID %>"); |
| 77 | + if (this.checked) { |
| 78 | + selectAllItems(multiSelect); |
| 79 | + } else { |
| 80 | + deselectAllItems(multiSelect); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + function selectAllItems(multiSelect) { |
| 86 | + var $ = $telerik.$; |
| 87 | + var values = $.map(multiSelect.get_itemsData(), function (dataItem) { |
| 88 | + return dataItem.text; // Use the 'text' field from itemsData |
| 89 | + }); |
| 90 | + multiSelect.set_value(values); // Select all items |
| 91 | + } |
| 92 | + |
| 93 | + function deselectAllItems(multiSelect) { |
| 94 | + multiSelect.set_value([]); // Deselect all items |
| 95 | + } |
| 96 | +</script> |
| 97 | +``` |
| 98 | + |
| 99 | +By following these steps, you can easily add a "Select All" checkbox to the `RadMultiSelect` component, enhancing user interaction and selection efficiency within your ASP.NET AJAX applications. |
| 100 | + |
| 101 | +## See Also |
| 102 | + |
| 103 | +- [RadMultiSelect Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/multiselect/overview) |
| 104 | +- [Client-Side Programming Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/multiselect/client-side-programming/overview) |
0 commit comments