Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 309338 - Last Review: January 26, 2004 - Revision: 3.3
The Attributes property of a ListItem control only works within an HtmlSelect control
This article was previously published under Q309338
When you use the
RadioButton or
DropDownList control, if you try to place a key-value pair in the
ListItem control, the key-value pairs are not written to the result page.
For example, the following
RadioButtonList code
<asp:RadioButtonList id="RadioButtonList1" runat="server">
<asp:ListItem Value="v1" Key="k1">1</asp:ListItem>
</asp:RadioButtonList>
-or-
RadioButtonList1.Items[0].Attributes.Add("key", "k1");
only returns the following output:
<input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="v1" />
<label for="RadioButtonList1_0">1</label>
Similarly, the following
DropDownList code
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="v1" Key="k1">1</asp:ListItem>
</asp:DropDownList>
only returns the following output:
<option value=v1>1</option>
The "Key=k1" is not rendered.
The
ListItem control has a property
Attributes of type
AttributeCollection. When the
ListItem control is inside a
RadioButtonList control or a
DropDownList control, key-value pairs that you assign to the
Attributes property of the
ListItem control (either programmatically or declaratively) are not rendered.
To work around this problem in the
RadioButtonList control, use a group of
HtmlInputRadioButton controls. For example:
<input id="Radio01" type="radio" name="group1" Key="k1" runat="server" >
To work around this problem in the
DropDownList control, use the
HtmlSelect control. For example:
<select runat="server">
<option value="v1" id="ID1" key="k1">1</option>
</select>
This behavior is by design.
In the "Resolution" section of this article, the
HtmlSelect control was used to show how to implement expando attributes as a workaround for using the
DropDownList control. While both classes do make use of the
ListItem class internally, they support expando attributes differently. The
HtmlSelect control does support using expando attributes because its programming model is more HTML-centric. However, supporting expando attributes with the
DropDownList control is more involved than just rendering the attributes themselves. For example, you cannot use data-binding to support attributes with the
DropDownList control.
APPLIES TO
- Microsoft ASP.NET 1.0
- Microsoft ASP.NET 1.1
| kbprb kbreadme kbservercontrols KB309338 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate