views:

95

answers:

3

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!

Any help appreciated - cheers.

James

A: 

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.

Elangovan
So wheather I can get to the data or not is regardless at the moment because even if I apply some sort of styling to the contents it will have no effect?
james lewis
abatishchev's solution works if you want to make the particular entry with bold font. But if you want to change the partial text, then it cannot be done using server side control.
Elangovan
@Elangovan: I think this depends how does the text renders - how does it look in raw HTML. If you can use HTML markup as an inner text of control - than you, probably, should be able to make be bold a particular text. I.e. it will look like this: `<span>My <b>particular</b> text</span>`
abatishchev
Standard html drop-down list supports color styling, this will work :<option style="background-color:Black;color:White">test</option>
MK
+1  A: 

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.

abatishchev
OK so back to the RenderContents method of my control... surely this outputs HTML for each item in the list? Therefore based on what you've said I should be able to apply a style to each individual item?
james lewis
Any server-side control becomes an HTML one after render. So you can apply any appropriate HTML method/tool/approach to it
abatishchev
A: 

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...

Any thoughts on that?

James

james lewis
I'd separate our the description and IsUsed fields and then you won't need to split the string in the RenderContents. **You don't want your data layer worrying about presentation**. Is there any reason you can't have the third field being returned?
senloe
It's the code in the custom control that splits the string but I see your point. I just don't know enough about data binding - so it doesn't matter how many fields my SP returns, because I can just pick and choose what to do with them in the dropdownlists code correct? I'll give this a go when I'm in the office tomorrow, definately more elegant than a delimited string!
james lewis
Doesn't look like I can do it this way... In RenderContents I only seem to have access to the ddl's Items property - the items just have the description and id returned from the stored proc, the 3rd field I just added is ignored...
james lewis