Skip to content

Commit 141a879

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article combobox-styling-specific-words-in-items (#617)
Co-authored-by: KB Bot <[email protected]>
1 parent f63d069 commit 141a879

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Styling Specific Words in RadComboBox Items
3+
description: Learn how to apply bold or italic styling to specific words within the items of a RadComboBox for ASP.NET AJAX.
4+
type: how-to
5+
page_title: Applying Custom Styling to Parts of RadComboBox Items
6+
slug: combobox-styling-specific-words-in-items
7+
tags: radcombobox, asp.net ajax, itemtemplate, styling, bold, italic
8+
res_type: kb
9+
ticketid: 1668235
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+
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 `<i>` or `<b>` tags directly in the item text shows the tags instead of applying the styling.
29+
30+
This KB article also answers the following questions:
31+
- How to apply bold styling to part of a RadComboBox item text?
32+
- How to italicize a portion of the text in RadComboBox items?
33+
- How to use templates to customize the appearance of RadComboBox items?
34+
35+
## Solution
36+
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.
37+
38+
Here is an example of how to achieve this:
39+
40+
Define the `RadComboBox` with an `ItemTemplate` on your ASPX page:
41+
42+
````ASP.NET
43+
<telerik:RadComboBox ID="RadComboBox1" runat="server" RenderMode="Lightweight" Width="400px" EnableLoadOnDemand="true"
44+
OnItemsRequested="RadComboBox1_ItemsRequested" OnClientDropDownClosing="styleItems" OnClientBlur="styleItems" HighlightTemplatedItems="true">
45+
<ItemTemplate>
46+
<%# Container.Text %>
47+
</ItemTemplate>
48+
</telerik:RadComboBox>
49+
````
50+
51+
In your code-behind, add items with styled text to the RadComboBox during the `ItemsRequested` event:
52+
53+
````C#
54+
protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
55+
{
56+
RadComboBox combo = sender as RadComboBox;
57+
58+
RadComboBoxItem item = new RadComboBoxItem
59+
{
60+
Text = "My <b>bold</b> item",
61+
Value = "1"
62+
};
63+
combo.Items.Add(item);
64+
65+
item = new RadComboBoxItem
66+
{
67+
Text = "My <i>italic</i> item",
68+
Value = "2"
69+
};
70+
combo.Items.Add(item);
71+
72+
combo.DataBind();
73+
}
74+
````
75+
76+
Use JavaScript to ensure the styled text remains when an item is selected:
77+
78+
````JavaScript
79+
function styleItems(sender, args) {
80+
let inputElement = sender.get_inputDomElement();
81+
let escapedValue = removeHtmlTags(inputElement?.value);
82+
83+
setTimeout(() => {
84+
inputElement.value = escapedValue;
85+
});
86+
}
87+
88+
function removeHtmlTags(input) {
89+
return input.replace(/<[^>]*>/g, "");
90+
}
91+
````
92+
93+
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.
94+
95+
## See Also
96+
- [RadComboBox Templates Demo](https://demos.telerik.com/aspnet-ajax/dropdownlist/examples/functionality/templates/defaultcs.aspx)
97+
- [Client-Side Templates Demo](https://demos.telerik.com/aspnet-ajax/dropdownlist/examples/functionality/clienttemplates/defaultcs.aspx)
98+
- [RadComboBox Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview)

0 commit comments

Comments
 (0)