Manoj Singh
Manoj Singh

Reputation: 7707

how to color listitem in list

Hi I am using asp:listbox in my code.

  <td>
   <asp:ListBox id="ddlWhereStudy" runat="server" rows="4"></asp:ListBox>
    </td>

I want to color the listbox listitem conditionally. Please see the below code:

private void FillStudyWhereDropDown()       
{
                  XmlNodeList objNodeList = FinalDoc.SelectNodes("//root/tcm:ListKeywords[@Type='StudyWhere']/child::tcm:Item", namespaceManager);
                  ddlWhereStudy.Items.Clear();
                  ddlWhereStudy.Items.Add(new ListItem(ResourceFile.GetResourceString("c_AdvisorOptionDefault")));
                  for (int i = 0; i < objNodeList.Count; i++)
                  {
                        string[] parts = objNodeList[i].Attributes["Title"].Value.Split('_');
                        ListItem li = new ListItem(parts[1], parts[2]);
                        ddlWhereStudy.Items.Add(li);
                        if (parts[3] == "B")
                        {
                              li.Attributes.Add("Style", "Color: 'RED'");          
                        }
                  }
}           

Above code is working fine when I am using select but it is not working with asp:ListBox

Please suggest!

Upvotes: 1

Views: 7934

Answers (1)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

from this code list item 4 and 8 are red and others have default color

for (int count = 0; count < 10; count++)
    {
        ListItem li = new ListItem();
        li.Text = count.ToString();
        li.Value = count.ToString();
        if (count == 4 || count == 8)
        {
            li.Attributes.Add("style", "Color: Red");
        }
        lst.Items.Add(li);
    }

Upvotes: 4

Related Questions