Different styles in DropDownList items?

Discussion in 'ASP .Net' started by Gandalf, Jul 9, 2003.

  1. Gandalf

    Gandalf Guest

    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, Jul 9, 2003
    #1
    1. Advertising

  2. 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.selectedIndex);")
    sb.Append("</script>")
    Page.RegisterStartupScript("bgstart", sb.ToString)
    lstArtTipo.Attributes.Add("onchange", _
    "docolorddl(this.selectedIndex);")
    End Sub

    "Gandalf" <> wrote in message
    news:...
    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], Jul 9, 2003
    #2
    1. Advertising

  3. Gandalf

    Teemu Keiski Guest

    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.

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

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

    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com

    Email:



    "Gandalf" <> kirjoitti viestissä
    news:...
    > 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, Jul 9, 2003
    #3
  4. Gandalf

    Gandalf Guest

    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]" <> wrote in message
    news:...
    > 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.selectedIndex);")
    > sb.Append("</script>")
    > Page.RegisterStartupScript("bgstart", sb.ToString)
    > lstArtTipo.Attributes.Add("onchange", _
    > "docolorddl(this.selectedIndex);")
    > End Sub
    >
    > "Gandalf" <> wrote in message
    > news:...
    > 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, Jul 18, 2003
    #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. Chris
    Replies:
    3
    Views:
    462
    Chris
    Oct 27, 2004
  2. =?Utf-8?B?TmVpbCBTdGV2ZW50b24=?=

    Applying CSS Styles to Dropdownlist

    =?Utf-8?B?TmVpbCBTdGV2ZW50b24=?=, May 15, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    61,136
    Juan T. Llibre
    May 15, 2006
  3. Jeff
    Replies:
    2
    Views:
    5,174
  4. Nicolas Verhaeghe

    Mixing font styles in a dropdownlist

    Nicolas Verhaeghe, Jun 22, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    1,005
    Alexey Smirnov
    Jun 22, 2009
  5. Chirag Malvi

    different color of items in Dropdownlist control

    Chirag Malvi, Jul 17, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    Chirag Malvi
    Jul 17, 2003
Loading...

Share This Page