From 85b8e561f7549440cc012e4e20044fda3dd12d8d Mon Sep 17 00:00:00 2001 From: KB Bot Date: Thu, 24 Oct 2024 08:15:20 +0000 Subject: [PATCH] Added new kb article combobox-styling-specific-words-in-items --- ...ombobox-styling-specific-words-in-items.md | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 knowledge-base/combobox-styling-specific-words-in-items.md diff --git a/knowledge-base/combobox-styling-specific-words-in-items.md b/knowledge-base/combobox-styling-specific-words-in-items.md new file mode 100644 index 0000000000..505d8f3a12 --- /dev/null +++ b/knowledge-base/combobox-styling-specific-words-in-items.md @@ -0,0 +1,98 @@ +--- +title: Styling Specific Words in RadComboBox Items +description: Learn how to apply bold or italic styling to specific words within the items of a RadComboBox for ASP.NET AJAX. +type: how-to +page_title: Applying Custom Styling to Parts of RadComboBox Items +slug: combobox-styling-specific-words-in-items +tags: radcombobox, asp.net ajax, itemtemplate, styling, bold, italic +res_type: kb +ticketid: 1668235 +--- + +## Environment + + + + + + + + + + + + +
ProductRadComboBox for ASP.NET AJAX
VersionAll
+ +## Description +I am binding items in the code behind in a RadComboBox and using RadSkinManager to set the skin to Bootstrap. The items are rendering fine, but I want to style specific words within an item description to be italic or bold. Using `` or `` tags directly in the item text shows the tags instead of applying the styling. + +This KB article also answers the following questions: +- How to apply bold styling to part of a RadComboBox item text? +- How to italicize a portion of the text in RadComboBox items? +- How to use templates to customize the appearance of RadComboBox items? + +## Solution +To style specific parts of the text in a RadComboBox item, use the `ItemTemplate` feature. This approach allows you to define a custom template for the items, where HTML tags can be used to apply the desired styling. + +Here is an example of how to achieve this: + +Define the `RadComboBox` with an `ItemTemplate` on your ASPX page: + +````ASP.NET + + + <%# Container.Text %> + + +```` + +In your code-behind, add items with styled text to the RadComboBox during the `ItemsRequested` event: + +````C# +protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e) +{ + RadComboBox combo = sender as RadComboBox; + + RadComboBoxItem item = new RadComboBoxItem + { + Text = "My bold item", + Value = "1" + }; + combo.Items.Add(item); + + item = new RadComboBoxItem + { + Text = "My italic item", + Value = "2" + }; + combo.Items.Add(item); + + combo.DataBind(); +} +```` + +Use JavaScript to ensure the styled text remains when an item is selected: + +````JavaScript +function styleItems(sender, args) { + let inputElement = sender.get_inputDomElement(); + let escapedValue = removeHtmlTags(inputElement?.value); + + setTimeout(() => { + inputElement.value = escapedValue; + }); +} + +function removeHtmlTags(input) { + return input.replace(/<[^>]*>/g, ""); +} +```` + +This solution allows you to display RadComboBox items with specific words styled in bold or italic. However, once an item is selected, the styling will be removed in the input element due to HTML restrictions. To maintain styling after selection, consider using CSS or other styling methods that do not rely on HTML tags within the input element itself. + +## See Also +- [RadComboBox Templates Demo](https://demos.telerik.com/aspnet-ajax/dropdownlist/examples/functionality/templates/defaultcs.aspx) +- [Client-Side Templates Demo](https://demos.telerik.com/aspnet-ajax/dropdownlist/examples/functionality/clienttemplates/defaultcs.aspx) +- [RadComboBox Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview)