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.
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...
> 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...
I suspect you've got a bug there.
Ken
"Victor Garcia Aprea" <v...@NOobiesSPAM.com> wrote in message
news:OfanMcvzBHA.1512@tkmsftngp05...
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...
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...
--
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...