Different Background Colors for ListBox Items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gnosys@gmail.com

    #1

    Different Background Colors for ListBox Items

    In ASP.Net 1.1 using C#, I'm trying to dynamically change the
    background colors of certain listbox items based on some criteria. For
    example:

    [ Select Items ]
    [Item 1]
    [Item 2]
    [Item 3 (Hot) ]
    [Item 4]
    [Item 5 (Hot ]

    In the ListBox above, I would like the Items that are "Hot" to have a
    red background. This box will obviously be populated from a dataset,
    and my code time is like this:

    foreach (myItem i in myItems)
    {
    ListItem li = myListBox.FindB yValue(i.itemVa lue);
    if (!i.IsHot)
    continue;

    if (li != null)
    li.Attributes.C ssStyle.Add("ba ckground-color", "yellow");
    }

    No Compilation errors, I have tried other ways to do this, didn't
    work. However, if I just do ListBox.Attribu tes.CssStyle - it changes
    the bg color of all the items. I only need to change the background of
    certain items. Any help will be appreciated.
    }
  • EdisonCPP

    #2
    Re: Different Background Colors for ListBox Items

    See if this helps:


    //Relevant code:
    ddlMultiColor.I tems[row].Attributes.Add ("style",
    "background-color:" + ddlMultiColor.I tems[row].Value);





    <gnosys@gmail.c omwrote in message
    news:f5615e0e-f636-4ed4-886a-432a6312e4de@v1 3g2000pro.googl egroups.com...
    In ASP.Net 1.1 using C#, I'm trying to dynamically change the
    background colors of certain listbox items based on some criteria. For
    example:
    >
    [ Select Items ]
    [Item 1]
    [Item 2]
    [Item 3 (Hot) ]
    [Item 4]
    [Item 5 (Hot ]
    >
    In the ListBox above, I would like the Items that are "Hot" to have a
    red background. This box will obviously be populated from a dataset,
    and my code time is like this:
    >
    foreach (myItem i in myItems)
    {
    ListItem li = myListBox.FindB yValue(i.itemVa lue);
    if (!i.IsHot)
    continue;
    >
    if (li != null)
    li.Attributes.C ssStyle.Add("ba ckground-color", "yellow");
    }
    >
    No Compilation errors, I have tried other ways to do this, didn't
    work. However, if I just do ListBox.Attribu tes.CssStyle - it changes
    the bg color of all the items. I only need to change the background of
    certain items. Any help will be appreciated.
    }

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: Different Background Colors for ListBox Items

      gnosys@gmail.co m wrote:
      In ASP.Net 1.1 using C#, I'm trying to dynamically change the
      background colors of certain listbox items based on some criteria. For
      example:
      >
      [ Select Items ]
      [Item 1]
      [Item 2]
      [Item 3 (Hot) ]
      [Item 4]
      [Item 5 (Hot ]
      >
      In the ListBox above, I would like the Items that are "Hot" to have a
      red background. This box will obviously be populated from a dataset,
      and my code time is like this:
      >
      foreach (myItem i in myItems)
      {
      ListItem li = myListBox.FindB yValue(i.itemVa lue);
      if (!i.IsHot)
      continue;
      >
      if (li != null)
      li.Attributes.C ssStyle.Add("ba ckground-color", "yellow");
      }
      >
      No Compilation errors, I have tried other ways to do this, didn't
      work. However, if I just do ListBox.Attribu tes.CssStyle - it changes
      the bg color of all the items. I only need to change the background of
      certain items. Any help will be appreciated.
      }
      Check if your code actually causes any style to be set on the <option>
      tags in the resulting code.

      You should know that the support for styling a select is limited, and
      varies from browser to browser. Unless you are building an intranet
      where you can impose the usage of a specific browser, you should only
      use the style as a visual aid, i.e. also change the actual text of the
      item to make sure that all users can see the status.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      Working...