Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

transatlantys hot news

Contact

Me

Others

Selected content

DropDownList with custom attributes on the items

I've created a custom DropDownList web control that enables the use of custom attributes on its items. This is just a rather simple improvement over the standard .NET web control, but a useful one in my opinion.

For example, this control can be used as follows:

<mad:DropDownList id="ddlTest" runat="server">
  <asp:ListItem Value="">An
</asp:ListItem>
  <asp:ListItem Value="" style="color:red;font-weight:bold;text-align:center">invalid
</asp:ListItem>
  <asp:ListItem Value="">item
</asp:ListItem>
  <asp:ListItem Value="">is in here
</asp:ListItem>
</mad:DropDownList>

The result being this:

The source code is available of course.
Warning: not all CSS attributes work with Internet Explorer. This screenshot comes from Phoenix, but at least the color attribute works with IE.

Comments

steven_zhou said:

how to use this control?
# August 11, 2003 9:56 PM

Patrik said:

Do you also work with Visual Studio? If you have <mad:DropDownList> and within <asp:ListItem>, do you also get the warning "The active schema does not support the element 'asp:ListItem'."?
# August 13, 2003 7:30 AM

Fabrice said:

For those who ask how to use this WebControl: like with any other webcontrol, you just have to add the source file to a class library, and add this library to the toolbox.
# August 31, 2003 8:16 AM

Fabrice said:

Patrik, I don't remember, but I think it can happen. I don't really know what needs to be done to remove this warning.
# August 31, 2003 8:18 AM

Sean said:

Its a pity the ListItemCollection doesn't save the attributes in its viewstate. Attributes aren't saved on postback.
# October 29, 2003 10:56 PM

Fabrice said:

Sean, that can probably be fixed in the derived class.
# November 9, 2003 10:36 AM

Vic said:

Does anybody have the code in vb.net?  Please post if you do.  Thank You.

# January 10, 2007 1:10 PM

Fabrice Marguerie said:

Oh come on Vic, it's such a short code!

# January 10, 2007 1:29 PM

lamia said:

how to get your DropdownList like this( green border color )?

thank you

# April 23, 2007 11:27 AM

Fabrice Marguerie said:

The green color comes from the Windows theme used on the machine. You can specify colors using CSS, but you may not be able to change the color or everything, especially for all web browsers...

# April 23, 2007 12:25 PM

tinker said:

About the border and arrow color for the Dropdown - could you possibly explain the skin/theme or show it to us. Default XP blue (even on Vista) clashes with the background I want to use, and nothing I am doing to try to change the dropdown colors is making any difference... Thanks.
# May 12, 2007 4:44 AM

Francesco Geri said:

La propriet

# July 21, 2008 8:34 AM

Tuba said:

Hi,

I love this code but there's a problem.. I'm not working this.. Could you help me, pls..

Html source is not true ?...

Thanks for your share...

Have a nice day...

# May 13, 2009 2:45 AM

Gr8Onkar said:

Hi Fabrice,

Can we partially format drop-down list text?

For ex, 'Invalid' item text should be formatted as 'In' in 'Red' color and 'valid' in 'Blue' color.

# May 13, 2009 3:03 AM

Fabrice Marguerie said:

Tuba: I've fixed the markup. I don't know why it became messed up.

Gr8Onkar: Have you tried with multiple spans in a ListItem?

# May 13, 2009 4:58 AM

yevi said:

Very good.

Is it possible using your approach, to disable an item?

Making it "locked", no letting click on this item with a mouse or selecting it?

(The item should appear in the list, gray colored)

Thanks.

# May 16, 2009 12:34 PM

Fabrice Marguerie said:

yevi: the disabled attribute is the solution, except it doesn't work in Internet Explorer.

Other solutions: forums.digitalpoint.com/showthread.php

# May 16, 2009 12:50 PM

yevi said:

Fabrice Marguerie, can't it be done by extending the asp:dropdownlist?

# May 16, 2009 3:45 PM

yevi said:

I want a  Dropdownlost because I need the binding stuff and more built in features.

Using html dropdownlist will require to implement all that from scratch.

# May 16, 2009 3:51 PM

Jackson said:

Can I add Image instead of Text in Drop-Down list?

# September 26, 2009 8:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)