Formatting a ListItem Control

N

Nathan Sokalski

I want to change the background color, font attributes, etc. of the choices
in my DropDownLists. When writing them using HTML SELECT and OPTION tags, I
can do something like the following:

<option
style="font-weight:bold;color:green;background-color:purple;">Displayed
Text</option>

but the ListItem Control does not have a Style property, which prevents me
from doing anything other than specifying the Text, Value, and Selected
properties. Is there any way to format the ListItem Control, such as some
kind of workaround (I know I could write my own Control that inherits
ListItem, but I have very little experience doing this, so I might do it in
the future, but for the moment I am looking for a workaround)?
 
G

Graham

Afternoon Nathan
Im not sure how frowned upon the practise of stylising individual options
within a select is but:
In the html code you could just add the style tag to the listitem
declaration (sure, its not there in intellisense but it should still show up
in ur broswer):
<asp:dropdownlist id="foo" runat="server">
<asp:listitem value="bar" style="background: #000000;">Bar it
up</asp:listitem>
</asp:dropdownlist>

or if you are using code to add items to the dropdownlist, then you could
add attributes to each listitem:
ListItem li = new ListItem();
li.Value = "bar";
li.Text = "Bar it up";
li.Attributes.Add("style", "background: #000000;");
Foo.Items.Add(li);

Also be aware that some css styles will not work when applied to
dropdownlists and possibly their child listitems.

Graham
 
N

neilmcguigan

you can do it using a style sheet like this:

<html>
<head>

<style>
..DropDownList option
{
font-weight:bold;color:green;background-color:purple;
}
</style>

</head>
<body>
<form runat="server">
<asp:DropDownList id="DropDownList1" runat="server"
CssClass="DropDownList"></asp:DropDownList>
</form>
</body>
</html>
 
S

Scott Mitchell [MVP]

Nathan said:
I want to change the background color, font attributes, etc. of the choices
in my DropDownLists. When writing them using HTML SELECT and OPTION tags, I
can do something like the following:

<option
style="font-weight:bold;color:green;background-color:purple;">Displayed
Text</option>

but the ListItem Control does not have a Style property, which prevents me
from doing anything other than specifying the Text, Value, and Selected
properties. Is there any way to format the ListItem Control, such as some
kind of workaround (I know I could write my own Control that inherits
ListItem, but I have very little experience doing this, so I might do it in
the future, but for the moment I am looking for a workaround)?

Nathan, this is a known problem with ListControls in ASP.NET 1.x. Check
oiut this article for a discussion as to WHY and a potential workaround.
(Although if all you want to do is styling, then the solution posted
by (e-mail address removed) is probably easier than my workaround at the
article below...)

ListControl Items and Attributes
http://aspnet.4guysfromrolla.com/articles/091405-1.aspx

Happy Programming!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top