WebControls 中 RadioButton or DropDownList control 的 ListItem中的Attributes原来无法使用。

The Attributes property of a ListItem control only works within an HtmlSelect controlli = new ListItem(dt.Rows[i]["PName"].ToString()+"()",
The Attributes property of a ListItem control only works within an HtmlSelect control                                    dt.Rows[i][
"PName"].ToString()+"()");
The Attributes property of a ListItem control only works within an HtmlSelect control            
The Attributes property of a ListItem control only works within an HtmlSelect control                li.Attributes.Add(
"optType","selfFun");
The Attributes property of a ListItem control only works within an HtmlSelect control                li.Attributes.Add(
"title",dt.Rows[i]["PDesc"].ToString());
The Attributes property of a ListItem control only works within an HtmlSelect control
上述属性怎样也无法显示.  Microsoft 回复如下
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 >
<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  />
<label for="RadioButtonList1_0">1</label>
Similarly, the following DropDownList code
<asp:DropDownList >
<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 Attributes property of a ListItem control only works within an HtmlSelect controlBack to the top

CAUSE

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.

The Attributes property of a ListItem control only works within an HtmlSelect controlBack to the top

RESOLUTION

To work around this problem in the RadioButtonList control, use a group of HtmlInputRadioButton controls. For example:
<input  >
To work around this problem in the DropDownList control, use the HtmlSelect control. For example:
<select runat="server">
<option value="v1" >1</option>
</select>

The Attributes property of a ListItem control only works within an HtmlSelect controlBack to the top

STATUS

This behavior is by design.

The Attributes property of a ListItem control only works within an HtmlSelect controlBack to the top

MORE INFORMATION

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.

The Attributes property of a ListItem control only works within an HtmlSelect controlBack to the top


APPLIES TO
Microsoft ASP.NET 1.0
Microsoft ASP.NET 1.1


参考: http://support.microsoft.com/default.aspx?scid=kb;en-us;309338&Product=NETFrame


解决办法:

把上述RadioButton or DropDownList control换成HtmlInputRadioButtonHtmlSelect  控件即可

相关文章:

  • 2021-07-07
  • 2022-12-23
  • 2021-10-21
  • 2021-08-12
  • 2021-05-25
  • 2021-10-07
  • 2021-12-04
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2021-11-27
  • 2022-12-23
  • 2021-07-20
相关资源
相似解决方案