Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug in DropDownList?

237 views
Skip to first unread message

Victor Garcia Aprea

unread,
Mar 18, 2002, 9:38:04 PM3/18/02
to
Hi All,

I'm not sure if this is the right place to post possible bugs, if not please
tell me where this should go.
By answering a question in the webcontrols newsgroups I found what I think
may be a bug.
I was trying to give a simple example on how to add an attribute to an
<option> element. But I didnt worked.
I tried:
[C#]
ListItem li = new ListItem("One","1");
li.Attributes.Add("style","background-color:red");
// dd1 is an instance of DropDownList class
dd1.Items.Add(li);

The ListItem class has an Attributes collection (I already know that style
object for the <option> element only accepts background-color and color and
ignores everything else) so I tought that when rendering its Items the
DropDownList class would also output the contents of this collection. But my
simple example was not working so I ended up dissasemblying the
RenderContents method of the DropDownList class and found that it output its
<option> elements with only a selected (based on the Selected property of
the ListItem) and a value attribute. There is no reference at all a the
Attributes collection of a ListItem.

I think this may be a bug and let MS know about it,

-Victor.

--
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.


Ken Cox [Microsoft MVP]

unread,
Mar 18, 2002, 10:27:40 PM3/18/02
to
You mean that the bug is in providing Attributes for list items?

Can individual list items have styles in HTML? Wouldn't the style be applied
to the control as a whole?

If so, this works:

DropDownList1.BackColor = Color.Red

Ken
MVP [ASP.NET]

Join the MVPs' effort to uncover new cancer drugs through computational
chemistry.
http://members.ud.com/services/teams/team.htm?id=A740D227-A950-475E-9543-A46
DD4C5A6EF


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim li As New ListItem()
li.Value = "1"
li.Text = "One"
DropDownList1.Items.Add(li)
DropDownList1.BackColor = Color.Red
End Sub

"Victor Garcia Aprea" <v...@NOobiesSPAM.com> wrote in message
news:#liY77uzBHA.2380@tkmsftngp04...

Victor Garcia Aprea

unread,
Mar 18, 2002, 10:35:49 PM3/18/02
to
Hi Ken,

> Can individual list items have styles in HTML? Wouldn't the style be
applied
> to the control as a whole?

Yes they can. I checked the html reference at msdn. The <option> element
supports a sytle object which in turns accepts background-color and color
and ignores everything else. So writing something like this:

> DropDownList1.BackColor = Color.Red
This will not help in setting each item individually to a different color.

This is an example:
<select>
<option style="color:red" value="1">One</option>
<option style="color:blue" value="1">One</option>
</select>

This actually gives you a different color for each item (I tried it and it
worked ok).

But as I posted before the RenderContents of DropDownList is just ignoring
the Attributes collection of the ListItem class.


-Victor.

--
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.


"Ken Cox [Microsoft MVP]" <BANSPAM...@sympatico.ca> wrote in message
news:OVOV4XvzBHA.2508@tkmsftngp03...

Ken Cox [Microsoft MVP]

unread,
Mar 18, 2002, 10:58:52 PM3/18/02
to
Right you are! I'd never seen multi-coloured list items in HTML! You learn
something every day.

I suspect you've got a bug there.

Ken

"Victor Garcia Aprea" <v...@NOobiesSPAM.com> wrote in message

news:OfanMcvzBHA.1512@tkmsftngp05...

Victor Garcia Aprea

unread,
Mar 19, 2002, 1:23:17 AM3/19/02
to
Hi Ken,

While we wait to confirm if that is a bug, I have written a modified version
of RenderContents:

[C#]
override protected void RenderContents(HtmlTextWriter writer)
{
for(int c=0;c<Items.Count;c++)
{
ListItem i = Items[c];
writer.WriteBeginTag("option");
if(i.Selected)
writer.WriteAttribute("selected","selected",false);
writer.WriteAttribute("value",i.Value,true);
IEnumerator d = Items[c].Attributes.Keys.GetEnumerator();
while(d.MoveNext())

writer.WriteAttribute(d.Current.ToString(),Items[c].Attributes[d.Current.ToS
tring()]);
writer.Write('>');
System.Web.HttpUtility.HtmlEncode(i.Text,writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}

Deriving a new class from DropDownList and overriding its RenderContents
method with the method above will output the Attributes collection.

-Victor.

--
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.


"Ken Cox [Microsoft MVP]" <BANSPAM...@sympatico.ca> wrote in message

news:ea#VUpvzBHA.2640@tkmsftngp07...

Ken Cox [Microsoft MVP]

unread,
Mar 19, 2002, 8:19:10 AM3/19/02
to
Did someone beat us to it?

BUG: Attributes Property of ListItem Only Works Within an HtmlSelect Control
(Q309338)

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338

The ListItem control has a property Attributes of type AttributeCollection .
When the ListItem is inside a RadioButtonList or a DropDownList , key-value
pairs that you assign to the Attributes property of the ListItem (either
programmatically or declaratively) are not rendered.


Ken

"Victor Garcia Aprea" <v...@NOobiesSPAM.com> wrote in message

news:u4Yvw5wzBHA.2520@tkmsftngp05...

Victor Garcia Aprea

unread,
Mar 19, 2002, 8:49:20 AM3/19/02
to
Yes they did, I have just finished reading the article your pointed out, and
it applies exactly to the case we found yesterday.

--
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.


"Ken Cox [Microsoft MVP]" <BANSPAM...@sympatico.ca> wrote in message

news:e6Buai0zBHA.2272@tkmsftngp02...

0 new messages