Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background Color; Listbox 2

Status
Not open for further replies.

Isadore

Technical User
Feb 3, 2002
2,167
US
I have wanted to give my listbox a dark background with light forecolor font but there seems to be a "seepage" of the background color outside the Listbox boundary. As any else noticed this, and if so, if there a solution to correct it?
 
Yeah, I know what you mean (it doesn't just colour the listitems, it colours the whole ListBox which is slightly larger than the list items themselves).

I can't think of a solution off the top of my head but I'm sure someone in the HTML/CSS forum (forum215) will know fairly quickly (it may be that you can apply the colours to the list itmes via CSS rather than to the whole listbox).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I knew I should have thought harder before posting that! You can probably do it quite easily through the style attribute of the ListItem e.g.
Code:
myListItem.Attributes.Add("style", "background-color: BLUE");
I haven't tested the above but hopefully it should work!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
cas - had a bit of trouble getting it to accept the add.attribute but the following was an improvement:

Add the CSS statement inside the <body> tag:
Code:
<style type="text/css">
  .listClass{background: #000080;}
</style>

And then add the reference to the asp Listbox:
Code:
<asp:ListBox id="myList" cssClass="listClass" .....

There was an improvement overall, with only a very slight seepage of color out of the bottom of the listbox, perhaps 2 pixels at most. Good idea using the CSS aproach, thanks.
 
had a bit of trouble getting it to accept the add.attribute
Ah, sorry my fault! I forgot that the list items will not render the style attribute. There is an article on this and workaround here:


There is also another alternative. Rather than use the ListBox server control, you could use a normal HTML control and this will render the style attribute. e.g.
Code:
<SELECT id="ListBox1" runat="server" Size="10">
</SELECT>
Then, you can just add the style attribute as I declared above (remember to add the declaration for the HTML control):
Code:
Protected WithEvents ListBox1 As System.Web.UI.HtmlControls.HtmlSelect

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim li As ListItem
        For i As Integer = 1 To 10
            li = New ListItem
            li.Text = i
            li.Value = i
            li.Attributes.Add("style", "background-color: BLUE")
            ListBox1.Items.Add(li)
            li = Nothing
        Next
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
ca8msm - Thanks for your time and trouble - this is a particularly interesting area (inlcuding the reference to the article on DataList, and the rendering differences -- great topic for the up and coming dot NET programmer. I appreciate your time and effot, thanks.
 
I had to do this once, and ended up using javascript to set the background color. :p

The CSS strategy is interesting. I'll have to check that out (is it possible to define checkbox style for a given element, not the whole page?).
 
is it possible to define checkbox style for a given element, not the whole page
Yes, I think there are a few ways of doing it.

1) You could say that you wanted all "option" (the list items themseleves) tags within a certain div to take on a certain style. e.g.
Code:
#myDiv option
{
	background-color: Blue	
}

2) You could give a class to each option tag
Code:
option.myClass
{
	background-color: Blue	
}

It may be wise to check with the CSS forum if these are the correct methods to use, but it hopefully it demonstrates that it can be done.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top