zoukankan      html  css  js  c++  java
  • The Attributes property of a ListItem control only works within an HtmlSelect control

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

    li = new ListItem(dt.Rows[i]["PName"].ToString()+"()",
                                        dt.Rows[i][
    "PName"].ToString()+"()");
                
                    li.Attributes.Add(
    "optType","selfFun");
                    li.Attributes.Add(
    "title",dt.Rows[i]["PDesc"].ToString());
    上述属性怎样也无法显示.  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 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.

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

    Back to the top

    RESOLUTION

    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>
    

    Back to the top

    STATUS

    This behavior is by design.

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

    Back 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  控件即可
  • 相关阅读:
    Android Preference 实现长按监听 longclickable
    表达式求值
    二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
    poj 1806
    树的公共祖先问题LCA
    给定两个长度相同,分别有序的数组A和B,求两个数组中所有数的中位数
    网络爬虫基本原理(转载)
    atoi函数的实现
    一个天平,12个大小,外观相同的球,一个球的重量与其他的不同,称3次找出问题小球
    动态规划求RMQ(区间最值问题Range Minimum/Maximum Query)
  • 原文地址:https://www.cnblogs.com/Bruce_H21/p/1100615.html
Copyright © 2011-2022 走看看