Different styles in DropDownList items?

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Different styles in DropDownList items?

    I wish to apply different styles to items in a DropDownList. However, any
    styles applied in the codebehind (using the ListItem.Addtributes property)
    do not get rendered. This is an old question but I've yet to see a
    satisfactory answer, other than it's a bug in the DropDownList. It seems to
    work for a HtmlSelect control, but this does not provide the AutoPostBack
    feature I require. Any help appreciated.
    --



    Gandalf Guest

  2. Similar Questions and Discussions

    1. #24095 [Com]: phpinfo styles override page styles and damage appearance
      ID: 24095 Comment by: joe at monkeydepartment dot com Reported By: generic at thetahat dot com Status: Open...
    2. Creating master page where global items arearranged ABOVE local items
      When you create a mater page, obviously all the items are arranged below anything on the individual pages. Is there a way of creating a master...
    3. possible for publishers to edit Library Items / recurrent items like navigation?
      No, you have to use an include of some sort. If you don't want to use SSI you can do it with Javascript. Search Google with the keyword "Javascript...
    4. changing type size in paragraph styles and how it effects nested styles
      In a book I'm developing, I decided to up the point size for my "first paragraph" and "body text" in my paragraph styles. I have nested styles in...
    5. HELP! DropDownList Items property in composite control -- collection editor support question!
      Hi, I have a composite web custom control consisting of a textbox, a button, and a dropdownlist.. I'm trying to expose the Items property from...
  3. #2

    Default Re: Different styles in DropDownList items?

    You pretty well have to do it in client-side script. Of course you can use the
    codebehind to output the client-side script like this:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Dim sb As New System.Text.StringBuilder
    If Not IsPostBack Then
    lstArtTipo.Items.Add("First item")
    lstArtTipo.Items.Add("Second item")
    lstArtTipo.Items.Add("Third item")
    lstArtTipo.Items.Add("Fourth item")
    End If
    sb.Append("<script language='javascript'>")
    sb.Append("function docolorddl(selI)")
    sb.Append("{document.Form1.lstArtTipo.options")
    sb.Append("[selI].style.backgroundColor=")
    sb.Append("'PowderBlue';}</script>")
    Page.RegisterClientScriptBlock("bgcolor", sb.ToString)
    sb.Length = 0
    sb.Append("<script language='javascript'>")
    sb.Append("docolorddl(document.Form1.lstArtTipo.se lectedIndex);")
    sb.Append("</script>")
    Page.RegisterStartupScript("bgstart", sb.ToString)
    lstArtTipo.Attributes.Add("onchange", _
    "docolorddl(this.selectedIndex);")
    End Sub

    "Gandalf" <gandalf@sauron.com> wrote in message
    news:OIZvNUcRDHA.3192@TK2MSFTNGP10.phx.gbl...
    I wish to apply different styles to items in a DropDownList. However, any
    styles applied in the codebehind (using the ListItem.Addtributes property)
    do not get rendered. This is an old question but I've yet to see a
    satisfactory answer, other than it's a bug in the DropDownList. It seems to
    work for a HtmlSelect control, but this does not provide the AutoPostBack
    feature I require. Any help appreciated.
    --




    Ken Cox [Microsoft MVP] Guest

  4. #3

    Default Re: Different styles in DropDownList items?

    I answered same kind of question on ASP.NET Forums, but on that occasion I
    instructed the user in developing custom control to overcome the problem.

    [url]http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=253555[/url]

    --
    Teemu Keiski
    MCP,Designer/Developer
    Mansoft tietotekniikka Oy
    [url]http://www.mansoft.fi[/url]

    ASP.NET Forums Moderator, [url]www.asp.net[/url]
    AspAlliance Columnist, [url]www.aspalliance.com[/url]

    Email:
    [email]joteke@aspalliance.com[/email]


    "Gandalf" <gandalf@sauron.com> kirjoitti viestissä
    news:OIZvNUcRDHA.3192@TK2MSFTNGP10.phx.gbl...
    > I wish to apply different styles to items in a DropDownList. However, any
    > styles applied in the codebehind (using the ListItem.Addtributes property)
    > do not get rendered. This is an old question but I've yet to see a
    > satisfactory answer, other than it's a bug in the DropDownList. It seems
    to
    > work for a HtmlSelect control, but this does not provide the AutoPostBack
    > feature I require. Any help appreciated.
    > --
    >
    >
    >

    Teemu Keiski Guest

  5. #4

    Default Re: Different styles in DropDownList items?

    Thanks Ken. WHat you are doing is setting the color of the selected item to
    powderblue, whereas I want each item in the dropdown to be a different
    color. I'm sure I could do it writing reams of jscript but i'd rather not.
    ;-)

    --

    "Ken Cox [Microsoft MVP]" <BANSPAMken_cox@sympatico.ca> wrote in message
    news:uY7jgpcRDHA.2332@TK2MSFTNGP10.phx.gbl...
    > You pretty well have to do it in client-side script. Of course you can use
    the
    > codebehind to output the client-side script like this:
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > Dim sb As New System.Text.StringBuilder
    > If Not IsPostBack Then
    > lstArtTipo.Items.Add("First item")
    > lstArtTipo.Items.Add("Second item")
    > lstArtTipo.Items.Add("Third item")
    > lstArtTipo.Items.Add("Fourth item")
    > End If
    > sb.Append("<script language='javascript'>")
    > sb.Append("function docolorddl(selI)")
    > sb.Append("{document.Form1.lstArtTipo.options")
    > sb.Append("[selI].style.backgroundColor=")
    > sb.Append("'PowderBlue';}</script>")
    > Page.RegisterClientScriptBlock("bgcolor", sb.ToString)
    > sb.Length = 0
    > sb.Append("<script language='javascript'>")
    > sb.Append("docolorddl(document.Form1.lstArtTipo.se lectedIndex);")
    > sb.Append("</script>")
    > Page.RegisterStartupScript("bgstart", sb.ToString)
    > lstArtTipo.Attributes.Add("onchange", _
    > "docolorddl(this.selectedIndex);")
    > End Sub
    >
    > "Gandalf" <gandalf@sauron.com> wrote in message
    > news:OIZvNUcRDHA.3192@TK2MSFTNGP10.phx.gbl...
    > I wish to apply different styles to items in a DropDownList. However, any
    > styles applied in the codebehind (using the ListItem.Addtributes property)
    > do not get rendered. This is an old question but I've yet to see a
    > satisfactory answer, other than it's a bug in the DropDownList. It seems
    to
    > work for a HtmlSelect control, but this does not provide the AutoPostBack
    > feature I require. Any help appreciated.
    > --
    >
    >
    >
    >

    Gandalf Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139