Click here to Skip to main content
6,632,253 members and growing! (17,896 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate License: The Code Project Open License (CPOL)

ListItem Control Background Formatting for DropDownList

By Roy Oliver

An article on a workaround for setting the background color of an ASP.NET DropDownList.
VB, Javascript, CSS, Windows, .NET 1.1, .NET 2.0, ASP.NET, WebForms, VS.NET2003, VS2005, IE 6.0, Dev
Posted:24 Feb 2006
Views:26,201
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.26 Rating: 2.10 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2

3
1 vote, 25.0%
4
1 vote, 25.0%
5

Introduction

If you have found this article then it means that you could not get the background style of a ListItem control to work properly with such web controls as a DropDownList. Yes, it's supposed to work because the ListItem control does contain an Attribute collection, but for some reason, none of the styles nor the supplied attributes will work. Well, a more tedious solution can be found here: List Control Items and Attributes by Scott Mitchell.

My solution, however, works just as well with one (known) flaw. The AutoPostBack property can not be used. I'm sure there are some JavaScript capabilities that can be implemented to duplicate this feature, so we won't consider it a major issue. At this point, you are probably wondering why the AutoPostBack property will be unavailable. Well my question to you is, "Why are you trying to use a DropDownList if its ListItem attributes don't work?".

If the handling of attributes for a DropDownList's ListItems are an obvious bug, then how about the ListItems for a System.Web.UI.HtmlControls.HtmlSelect control? I'm sure you know of all the ways to bind data with ASP.NET controls, so I'm positive that you'll use the simplicities of this example in more advanced ways.

ASPX

<form id="Form1" method="post" runat="server">
  <select name="ddlTest" id="ddlTest" runat="server" style="width:125px">
  </select>
</form>

Code Behind

Protected WithEvents ddlTest As System.Web.UI.HtmlControls.HtmlSelect

Private Sub Page_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Me.IsPostBack Then
        Dim l As ListItem

        l = New ListItem("Lions", "1")
        l.Attributes.Add("style", "background: yellow;")
        ddlTest.Items.Add(l)

        l = New ListItem("Tigers", "2")
        l.Attributes.Add("style", "background: orange;")
        ddlTest.Items.Add(l)

        l = New ListItem("Bears", "3")
        l.Attributes.Add("style", "background: brown;")
        ddlTest.Items.Add(l)

        l = New ListItem("Oh My!", "4")
        l.Attributes.Add("style", "background: red; color: white;")
        ddlTest.Items.Add(l)
    End If
End Sub

That's It

There's always a solution, even if it is just a quick fix.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Roy Oliver


Member

Occupation: Software Developer (Senior)
Company: CDC
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralThinking outside the box.. PinmemberAdam Joyce13:53 16 Sep '09  
GeneralRe: Thinking outside the box.. PinmemberRoy Oliver16:00 28 Sep '09  
GeneralThank you! / C# Pinmembermichaelgcooper18:08 5 May '09  
GeneralRe: Thank you! / C# PinmemberRoy Oliver15:24 7 May '09  
GeneralIt doesn't work if Autopost back is true Pinmembersumitranjansumit6:50 27 Aug '07  
Generalwhy not work? Pinmemberwuxsh1:07 16 May '06  
GeneralRe: why not work? PinmemberRoy Oliver15:08 17 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by Roy Oliver
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project