473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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.
--

Nov 17 '05 #1
3 5688
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" <ga*****@sauron.com> wrote in message
news:OI**************@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.
--


Nov 17 '05 #2
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.a...&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:
jo****@aspalliance.com
"Gandalf" <ga*****@sauron.com> kirjoitti viestissä
news:OI**************@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.
--

Nov 17 '05 #3
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]" <BA************@sympatico.ca> wrote in message
news:uY**************@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" <ga*****@sauron.com> wrote in message
news:OI**************@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.
--

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: mg | last post by:
I have a DataGrid (WebForm - C#) that has a template column that contains a dropdownlist named "DdlTest" In DataGrid1_UpdateCommand, the lin DropDownList ddlTest = (DropDownList)...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
6
by: tshad | last post by:
Is there a way to set one item in a dropdown list to a different color? I have a case where I might have 15 items and one is a default and would like to set the default as green where all the...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.