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!
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With