views:

267

answers:

1

Hi,

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!

A: 

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);
    }
Muhammad Akhtar
many thanks,is it working for asp:listbox?
MKS
yes. Plz try. and let me know if there is any problem
Muhammad Akhtar
Thank Akhtar, Do it will work in .net 1.1 version as it working fine in 2.0. Please confirm
MKS
I have not checked in 1.1 as I don't have 1.1 version, but I have checked in 2.0 and it is working
Muhammad Akhtar