|
| 1 | +--- |
| 2 | +title: Representing Comma-Separated List as Chips in RadChipList for ASP.NET AJAX |
| 3 | +description: Learn how to dynamically convert comma-separated values into chips using RadChipList for ASP.NET AJAX and bind the data effectively. |
| 4 | +type: how-to |
| 5 | +page_title: Dynamically Convert Comma-Separated Values into Chips in RadChipList |
| 6 | +slug: chiplist-comma-separated-list-as-chips |
| 7 | +tags: radchiplist, asp.net-ajax, createitem, dynamic-chips, binding-data |
| 8 | +res_type: kb |
| 9 | +ticketid: 1686711 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td> |
| 19 | +RadChipList for ASP.NET AJAX |
| 20 | +</td> |
| 21 | +</tr> |
| 22 | +<tr> |
| 23 | +<td>Version</td> |
| 24 | +<td>All</td> |
| 25 | +</tr> |
| 26 | +</tbody> |
| 27 | +</table> |
| 28 | + |
| 29 | +## Description |
| 30 | + |
| 31 | +I need to allow users to input values, like medical condition codes, in a text box and dynamically convert each value into a chip when followed by a comma. This functionality should reflect how Adobe Lightroom handles keywords for photos—users type a word followed by a comma, and it transforms into a chip. I also want to store and bind this data appropriately, considering the use of a comma-separated string. |
| 32 | + |
| 33 | +This knowledge base article also answers the following questions: |
| 34 | + |
| 35 | +- How to dynamically create chips in RadChipList for ASP.NET AJAX? |
| 36 | +- How to convert user input into RadChipList items with a text box? |
| 37 | +- How to bind data from RadChipList into a comma-separated string? |
| 38 | + |
| 39 | +## Solution |
| 40 | + |
| 41 | +To achieve this functionality, use the `RadChipList` control alongside a `RadTextBox` to dynamically create chips each time the user enters a value followed by a comma. The `createItem` method of the [RadChipList](https://www.telerik.com/products/aspnet-ajax/documentation/controls/chiplist/client-side-programming/methods#createitem) enables dynamic chip creation. Follow the steps below: |
| 42 | + |
| 43 | +1. Add a `RadTextBox` and a `RadChipList` to your page. |
| 44 | +2. Use the `OnKeyPress` client-side event of the `RadTextBox` to check for the comma character. |
| 45 | +3. When a comma is detected, extract the input, create a chip using `createItem`, and clear the text box for further input. |
| 46 | + |
| 47 | +````ASP.NET |
| 48 | +<telerik:RadTextBox RenderMode="Lightweight" ID="RadTextBox1" runat="server"> |
| 49 | + <ClientEvents OnKeyPress="onKeyPress" /> |
| 50 | +</telerik:RadTextBox> |
| 51 | +
|
| 52 | +<telerik:RadChipList runat="server" ID="ChipList1" /> |
| 53 | +```` |
| 54 | + |
| 55 | +````JavaScript |
| 56 | +let itemsArray = []; |
| 57 | + |
| 58 | +function onKeyPress(sender, args) { |
| 59 | + if (args.get_keyCharacter() === ',') { |
| 60 | + let code = sender.get_textBoxValue().trim(); // Get current input |
| 61 | + |
| 62 | + if (code) { |
| 63 | + let chipList = $find('<%= ChipList1.ClientID %>'); // Find RadChipList instance |
| 64 | + let selectedChip = chipList.createItem(code); // Create a chip with the input text |
| 65 | + |
| 66 | + itemsArray.push(selectedChip); // Add chip to array |
| 67 | + chipList.set_items(itemsArray); // Update the chip list |
| 68 | + |
| 69 | + setTimeout(() => { |
| 70 | + sender.set_textBoxValue(""); // Clear the text box for new input |
| 71 | + }, 10); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +```` |
| 76 | + |
| 77 | +Explanation: |
| 78 | + |
| 79 | +- The `OnKeyPress` event triggers each time a key is pressed within the `RadTextBox`. |
| 80 | +- The `args.get_keyCharacter()` method checks if the pressed key is a comma. |
| 81 | +- The `createItem` method dynamically creates a chip with the input text. |
| 82 | +- The `set_textBoxValue()` method clears the text box after the chip is created. |
| 83 | + |
| 84 | +## See Also |
| 85 | + |
| 86 | +- [RadChipList Documentation](https://www.telerik.com/products/aspnet-ajax/documentation/controls/chiplist/overview) |
0 commit comments