Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Formatting a ListItem Control

Reply
Thread Tools

Formatting a ListItem Control

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-11-2005
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-colorurple;">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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Graham
Guest
Posts: n/a
 
      10-11-2005
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" <(E-Mail Removed)> wrote in message
news:uz$(E-Mail Removed)...
>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-colorurple;">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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
 
 
 
neilmcguigan@gmail.com
Guest
Posts: n/a
 
      10-11-2005
you can do it using a style sheet like this:

<html>
<head>

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

</head>
<body>
<form runat="server">
<aspropDownList id="DropDownList1" runat="server"
CssClass="DropDownList"></aspropDownList>
</form>
</body>
</html>

 
Reply With Quote
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      10-11-2005
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-colorurple;">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 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!

--

Scott Mitchell [ASP.NET MVP]
(E-Mail Removed)
http://www.4GuysFromRolla.com/ScottMitchell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting text in the asp:ListItem control Nathan Sokalski ASP .Net 0 07-27-2007 04:33 PM
<asp:ListItem Value="0">blank choice</asp:ListItem> ? Jim in Arizona ASP .Net 7 11-22-2006 08:31 AM
Formatting a ListItem Control Nathan Sokalski ASP .Net 3 10-11-2005 03:58 PM
Formatting a ListItem Control Nathan Sokalski ASP .Net Web Controls 3 10-11-2005 03:58 PM
ropdownlist listitem.value not working - returns listitem.text instead Mad Scientist Jr ASP .Net 2 06-29-2004 01:13 PM



Advertisments