Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a DropDownList control display some items in bold ASP.NET

I'm working with a custom DropDownList control in ASP.Net and there's been a request to display certain items in the list with a bold typeface (NOTE - the control inherits from CompositeDataBoundControl so it can be data bound... not DropDownListBox). The control is bound to a table and there's a column in the table named IsUsed - if this is set to true, the corresponding item in the list should be rendered bold. (It should be noted here that this will only ever be viewed in FireFox.)

My experience is all in the middle \ backend tiers so the presentation layer is very new to me - can someone point me in the right direction? My initial thought was that somewhere in the custom control I would have access to all the rows that are returned from the data source which I could cycle through etc but I'm not sure if that's possible... There's also RenderContents which I can override... looks interesting!

like image 500
james lewis Avatar asked May 25 '10 09:05

james lewis


3 Answers

Here it is how to do what you need in code-behind:

var item = new ListItem("MyItem");
item.Attributes.Add("style", "font-weight: bold");

var list = FindControl("DropDownList1");
list.Items.Add(item);

Any control inherited from System.Web.UI.Control has property Attributes which you can use to add or append style attribute.

like image 51
abatishchev Avatar answered Nov 14 '22 09:11

abatishchev


Whatever the control that you are using in the server-side it will be rendered as a html in client browser, and the standard html drop-down list does not support styling its content. Instead of doing this you can go for JavaScript or jQuery custom drop-down list controls.

like image 45
Elangovan Avatar answered Nov 14 '22 08:11

Elangovan


OK I think I've answered my own question but it doesn't seem very elegant.

I can write a new stored proc to return the data I need to display in the list that will return ID and DESCRIPTION. However, description will be the description plus TRUE or FALSE (depending on the flag IsUsed in the table). Then in RenderContents I can split the description string, parse the bool and add a style attribute making the text bold if the bool is true...

like image 1
james lewis Avatar answered Nov 14 '22 08:11

james lewis