Alternating Item Style in a ListBox

R

Rob

Is there a way to set alternating item style for a ListBox? I know it
doesn't exist but is there a way to mimmick the Datagrid's
AlternatingItemStyle?

Thanks
Rob
 
J

Juan T. Llibre

If (e.Item.ItemType = ListItemType.AlternatingItem) Then
....set whatever style you want to
End If
 
R

Riki

Rob said:
Is there a way to set alternating item style for a ListBox? I know it
doesn't exist but is there a way to mimmick the Datagrid's
AlternatingItemStyle?

Here's a piece of code that does what you want (VB.NET):

Dim i As Integer
For i=0 To Listbox1.Items.Count-1 Step 2
ListBox1.Items(i).Attributes.Add("style","background-color: RED");
Next li
 
R

Rob

Thanks Riki...but it doesn't seem to be working. I tried this before and
it didn't work so I thought it wasn't possible. I tried it again and it
still doesn't work. Does this code look OK to you?


'I've already populated the dataset at this point.

lstCompanies.DataSource = ds
lstCompanies.DataTextField = "fullname"
lstCompanies.DataValueField = "company_id"
lstCompanies.DataBind()

lstCompanies.Items.Insert(0, New ListItem("--Select a buyer from the
list--"))

Dim i As Integer
For i = 0 To lstCompanies.Items.Count - 1 Step 2
lstCompanies.Items(i).Attributes.Add("style", "background-color: red")
Next

Rob
 
Joined
Mar 10, 2011
Messages
2
Reaction score
0
YOU CAN TRY THIS CODE :

CREATE A CSSCLASS FOR LISTBOX
LIKE
.listbox{ background-color:#e8ebd8;}

AND IN THE CODE BEHIND WRITE THIS

for (int i =1;i < ListBox1.Items.Count ;i+=2 )
{
ListBox1.Items.Attributes.Add("style", "Background-color:white" );
}
IT WILL WORK ,MINE IS WORKING .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top