Links:
view my trainer profile on TrainerExchange.com Listed on: Dmegs Web Directory

PHP video tutorials

phpmath.com
phpclasses.org
Canadian Mathematical Society
camel.math.ca

Algebra.com
Blogs:
rationalcode.com
Silverlight Playground

Technology:
silverlight.net
XNA Creators Club Online
php
asp.net


ASP.NET, Having items with different color in a ListBox or DropDownList

by Administrator 14. July 2009 02:41

One of my firends asked me "How can we change the text color for an individual item in a DropDownList or ListBox?". I thought it is a good idea to share the answer with you! The folowing image shows the dropDownList with the solution implemented. As you can see the First two items are red and the rest are black.

To implement it, follow these steps:

  1. Put a DropDownList control on your webform: <asp:DropDownList runat="server" Width="200" ID="lstAccounts" />
  2. On the Page_Load event handler add the following code
    1. // First items (Red)
      ListItem listItem1 = new ListItem("100000 (Active)", "100000");
      listItem1.Attributes.Add("style", "color: red;");
      lstAccounts.Items.Add(listItem1);
    2. // Second items (Red)
      ListItem listItem2 = new ListItem("100001 (Active)", "100000");
      listItem2.Attributes.Add("style", "color: red;");
      lstAccounts.Items.Add(listItem2);
    3. // Third items (Red)
      ListItem listItem3 = new ListItem("123456 (Active)", "100000");
      listItem3.Attributes.Add("style", "color: black;");
      lstAccounts.Items.Add(listItem3);
    4. // ...

You have also control over all attributes which are changable via CSS styles.

You can do the exact same thing with the ListBox object.

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

.NET | Training

Comments

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Reza Salehi
Microsoft Certified Professional

Microsoft Certified Trainer

Microsoft Certified Professional Developer

Microsoft certiied Technology Specialist (ASP.NET 2.0)

About Me

I started this blog to share my ideas, programs and interesting programming notes. Visitors who are interested in mathematics, physics and astronomy may find my blog interesting.

I live in Toronto, Canada and work as a Senior Software Developer and Microsoft Certified Trainer.

My online Microsoft web page is available  here.  Online business card.

RecentComments

Comment RSS