Different Background Colors for ListBox Items

Discussion in 'ASP .Net' started by gnosys@gmail.com, Oct 17, 2008.

  1. Guest

    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.FindByValue(i.itemValue);
    if (!i.IsHot)
    continue;

    if (li != null)
    li.Attributes.CssStyle.Add("background-color", "yellow");
    }

    No Compilation errors, I have tried other ways to do this, didn't
    work. However, if I just do ListBox.Attributes.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.
    }
     
    , Oct 17, 2008
    #1
    1. Advertising

  2. EdisonCPP Guest

    See if this helps:
    http://www.codeproject.com/KB/custom-controls/csMultiColorDropDownList.aspx?display=Print

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





    <> wrote in message
    news:...
    > 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.FindByValue(i.itemValue);
    > if (!i.IsHot)
    > continue;
    >
    > if (li != null)
    > li.Attributes.CssStyle.Add("background-color", "yellow");
    > }
    >
    > No Compilation errors, I have tried other ways to do this, didn't
    > work. However, if I just do ListBox.Attributes.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, Oct 17, 2008
    #2
    1. Advertising

  3. 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.FindByValue(i.itemValue);
    > if (!i.IsHot)
    > continue;
    >
    > if (li != null)
    > li.Attributes.CssStyle.Add("background-color", "yellow");
    > }
    >
    > No Compilation errors, I have tried other ways to do this, didn't
    > work. However, if I just do ListBox.Attributes.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
    _____
    http://www.guffa.com
     
    Göran Andersson, Oct 18, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kent
    Replies:
    1
    Views:
    894
    Andrew de la Harpe
    May 3, 2004
  2. Ken Cox [Microsoft MVP]

    Re: Listbox with colors as items

    Ken Cox [Microsoft MVP], May 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    364
    Ken Cox [Microsoft MVP]
    May 14, 2004
  3. Replies:
    6
    Views:
    610
    Hendrik van Rooyen
    May 31, 2007
  4. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    554
    darrel
    Aug 7, 2008
  5. ZelluX
    Replies:
    3
    Views:
    1,922
    Peter Otten
    Dec 1, 2008
Loading...

Share This Page