tags:

views:

1297

answers:

3

Hi,

I have a DropDownList control that is populated via server side.

I use a For Each [datarow] loop and create a New ListItem

In the DataRow is a column with the ID 'Title'; this field may contain either <B> or <I> tags. (ex. "[Title] in <i>[Parent Title]</i>")

The problem I am facing is when I [DropDownList].Controls.Add([ListItem]) it renders the text literally... so it displays the <B> or <I> tags literally and doesn't bold/italicize the font.

I have tried looking all over for an answer, can someone please point me in the right direction?

+1  A: 

You can try adding style attributes to each listitem, but I'm not sure how universally this is supported across different browsers.

ListItem li = new ListItem(dr["Name"], dr["Course"]);
if (bold) li.Attributes.Add("style", "font-weight:bold");
listcontrol.Items.Add(li);

Re-reading your question I noticed you want to mix normal and bold text within a single item in the list. I'm almost certain this isn't supported using the standard dropdown list. I think you'll need to have a look at using a CSS-based control that simulates a dropdown list.

Mike Powell
If you read my question I say that I am concerned about formatting font with html tags inside of the Text property, not formatting the *entire* ListItem. Please read more carefully in the future before answering.
John West
Duly noted. In the future, if I should decide to try to help you out again, I will be *sure* to read more carefully.
Mike Powell
Thank you for the update Mike; I was afraid of that...
John West
A: 

If you need to have the bold and italicized formatting, I'd recommend using an .net Literal control to serve as a placeholder for your drop down list. Then upon postback or databinding, you can iterate through your result set, generate the HTML code manually (since it is not that complex) and then insert the generated HTML into the Literal control.

Dillie-O
that didn't work :(a simple test to check yourself:<select><option>i am <b>bold</b></option></select>thanks for trying though :)
John West
Try using a CSS stylesheet/code for the ListItems in the list. Sadly though, most of the stuff I'm seeing indicates that you can only apply it to the entire entry, and it doesn't work in IE.
Dillie-O
+3  A: 

A DropDownList control is rendered as a select element in the hhtml code, and the ListItem controls are rendered as option elements. An option element does not support html formatting in the text. Some browsers may support some styling of the entire option text, but no browser supports styling of part of the text.

If you need html formatting in the dropdown list, you either have to make your own replacement dropdown list control using DHTML, or find someone who has done it already.

Guffa