473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Formatting a ListItem Control

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 Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
3 2385
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

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP09.phx.gbl...
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 Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #2
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>

Nov 19 '05 #3
Nathan Sokalski wrote:
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 ne**********@gmail.com is probably easier than my workaround at the
article below...)

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

Happy Programming!

--

Scott Mitchell [ASP.NET MVP]
mi******@4GuysFromRolla.com
http://www.4GuysFromRolla.com/ScottMitchell
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CGuy | last post by:
Hi, I have a RadioButtonList control in my aspx page which shows two radiobuttons - "Yes" and "No". Since my page is intended for international audience, I would like the texts "Yes" and "No" to...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
4
by: DBQueen | last post by:
I have a subform which is in Continuous Forms view. I have added a button to the bottom of the page to move to the next record using the button wizard (result: DoCmd.GoToRecord , , acNext). I...
7
by: Amil | last post by:
Hi all, I am using VS 2005 Beta 2. Which assembly (namespace?) should I use for the ListItem class? I don't have the Help files installed on my notebook for space reasons; I use MSDN2 online...
1
by: Mike P | last post by:
How do I manage to format a radiobuttonlist so that if for example I have 3 options, I can put a text box to the right of each option? The radiobutton list doesn't seem to accept any html inbetween...
4
by: Angela | last post by:
Hi everyone, At the moment I am using the syntax of: <asp:radiobuttonlist id="rdDrive" runat="server" RepeatLayout="Table" RepeatColumns="2"> <asp:ListItem Value="1"...
3
by: Joey | last post by:
Hi, I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default...
8
by: barry | last post by:
Have tried a number of things including space(n) in front of the string but when the DropDownList is shown the spaces have apparently been trimmed. Is this something that would have to be done in a...
1
by: dbuchanan | last post by:
I want to fill a textbox or a label or with lines from a listbox. I want to preserve the lline for line as it comes from the list box. I do not want the test to run-on. I suppose it is okay to wrap...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.