Formatting a ListItem Control

Discussion in 'ASP .Net Building Controls' started by Nathan Sokalski, Oct 11, 2005.

  1. I want to change the background color, font attributes, etc. of the choices
    in my DropDownLists. When writing them using HTML SELECT and OPTION tags, I
    can do something like the following:

    <option
    style="font-weight:bold;color:green;background-color:purple;">Displayed
    Text</option>

    but the ListItem Control does not have a Style property, which prevents me
    from doing anything other than specifying the Text, Value, and Selected
    properties. Is there any way to format the ListItem Control, such as some
    kind of workaround (I know I could write my own Control that inherits
    ListItem, but I have very little experience doing this, so I might do it in
    the future, but for the moment I am looking for a workaround)?
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Oct 11, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Graham Guest

    Afternoon Nathan
    Im not sure how frowned upon the practise of stylising individual options
    within a select is but:
    In the html code you could just add the style tag to the listitem
    declaration (sure, its not there in intellisense but it should still show up
    in ur broswer):
    <asp:dropdownlist id="foo" runat="server">
    <asp:listitem value="bar" style="background: #000000;">Bar it
    up</asp:listitem>
    </asp:dropdownlist>

    or if you are using code to add items to the dropdownlist, then you could
    add attributes to each listitem:
    ListItem li = new ListItem();
    li.Value = "bar";
    li.Text = "Bar it up";
    li.Attributes.Add("style", "background: #000000;");
    Foo.Items.Add(li);

    Also be aware that some css styles will not work when applied to
    dropdownlists and possibly their child listitems.

    Graham

    "Nathan Sokalski" <> wrote in message
    news:uz$...
    >I want to change the background color, font attributes, etc. of the choices
    >in my DropDownLists. When writing them using HTML SELECT and OPTION tags, I
    >can do something like the following:
    >
    > <option
    > style="font-weight:bold;color:green;background-color:purple;">Displayed
    > Text</option>
    >
    > but the ListItem Control does not have a Style property, which prevents me
    > from doing anything other than specifying the Text, Value, and Selected
    > properties. Is there any way to format the ListItem Control, such as some
    > kind of workaround (I know I could write my own Control that inherits
    > ListItem, but I have very little experience doing this, so I might do it
    > in the future, but for the moment I am looking for a workaround)?
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Graham, Oct 11, 2005
    #2
    1. Advertising

  3. Nathan Sokalski

    Guest

    you can do it using a style sheet like this:

    <html>
    <head>

    <style>
    ..DropDownList option
    {
    font-weight:bold;color:green;background-color:purple;
    }
    </style>

    </head>
    <body>
    <form runat="server">
    <asp:DropDownList id="DropDownList1" runat="server"
    CssClass="DropDownList"></asp:DropDownList>
    </form>
    </body>
    </html>
    , Oct 11, 2005
    #3
  4. Nathan Sokalski wrote:
    > I want to change the background color, font attributes, etc. of the choices
    > in my DropDownLists. When writing them using HTML SELECT and OPTION tags, I
    > can do something like the following:
    >
    > <option
    > style="font-weight:bold;color:green;background-color:purple;">Displayed
    > Text</option>
    >
    > but the ListItem Control does not have a Style property, which prevents me
    > from doing anything other than specifying the Text, Value, and Selected
    > properties. Is there any way to format the ListItem Control, such as some
    > kind of workaround (I know I could write my own Control that inherits
    > ListItem, but I have very little experience doing this, so I might do it in
    > the future, but for the moment I am looking for a workaround)?


    Nathan, this is a known problem with ListControls in ASP.NET 1.x. Check
    oiut this article for a discussion as to WHY and a potential workaround.
    (Although if all you want to do is styling, then the solution posted
    by is probably easier than my workaround at the
    article below...)

    ListControl Items and Attributes
    http://aspnet.4guysfromrolla.com/articles/091405-1.aspx

    Happy Programming!

    --

    Scott Mitchell [ASP.NET MVP]

    http://www.4GuysFromRolla.com/ScottMitchell
    Scott Mitchell [MVP], Oct 11, 2005
    #4
    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. Mad Scientist Jr
    Replies:
    2
    Views:
    429
    Mad Scientist Jr
    Jun 29, 2004
  2. Nathan Sokalski

    Formatting a ListItem Control

    Nathan Sokalski, Oct 11, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    535
    Scott Mitchell [MVP]
    Oct 11, 2005
  3. Jim in Arizona
    Replies:
    7
    Views:
    903
    Badass Scotsman
    Nov 22, 2006
  4. Nathan Sokalski

    Formatting text in the asp:ListItem control

    Nathan Sokalski, Jul 27, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    463
    Nathan Sokalski
    Jul 27, 2007
  5. Nathan Sokalski

    Formatting a ListItem Control

    Nathan Sokalski, Oct 11, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    149
    Scott Mitchell [MVP]
    Oct 11, 2005
Loading...

Share This Page