【问题标题】:How can I add a custom property to a RadioButtonList items?如何将自定义属性添加到 RadioButtonList 项?
【发布时间】:2012-11-25 23:25:51
【问题描述】:

如何将绑定Html5 data- attribute 添加到使用绑定RadioButtonList 生成的项目中?

我的代码如下所示:

<asp:Repeater Id="QuestionList" ...>
    <ItemTemplate>
        <asp:RadioButtonList DataSource='<%# Eval("Answers") %>'
                         SelectedValue='<%# Eval("SelectedAnswerId") %>'
                         DataTextField="Answer" 
                         DataValueField="AnswerId"
                         Tag='<%# Eval("QuestionId") %>' />
    </ItemTemplate>
</asp:Repeater>
var List<Question> questions = GetQuestions();
QuestionList.DataSource = questions;
QuestionList.DataBind();

它绑定到如下所示的类结构:

public class Question
{
    int QuestionId;
    string Question;
    List<Answer> Answers;
}

public class Answers
{
    int AnswerId;
    string Answer;
    bool SomeFlag;
}

我需要在 UI 中添加SomeFlag 以供 jQuery 使用,因此最终结果是生成的每个项目应如下所示:

<input type="radio" data-flag="true" ... />

有没有办法为绑定RadioButtonList生成的输入对象添加一个html数据属性?

【问题讨论】:

    标签: c# asp.net html radiobuttonlist


    【解决方案1】:

    您可以使用 ListItem 属性将自定义属性添加到单选按钮列表中的项目。您可以检查您的 html 是如何为单选按钮列表生成的,并让 jquery 为您获取所需的数据属性。

    在服务器端

    ListItem li1 = new ListItem();
    ListItem li2 = new ListItem();
    li1.Attributes.Add("data-flag", "true");
    li2.Attributes.Add("data-flag", "true");
    RadioButtonList1.Items.Add(li1);
    RadioButtonList1.Items.Add(li2);
    

    为单选按钮列表生成 html

    <table id="RadioButtonList1" border="0">
        <tr>
            <td><span data-flag="true"><input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="" /></span></td>
        </tr><tr>
            <td><span data-flag="true"><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="" /></span></td>
        </tr>
    </table>
    

    在 jquery 中访问

    $(':radio[id*=RadioButtonList1]').click(function(){
          alert($(this).closest('span').data('flag'));
    })
    

    【讨论】:

    • 有趣,以前没用。我想我没有在几个版本中尝试过!
    • +1 指出如何从 jQuery 访问它 :) 我花了一分钟才注意到属性被添加到 span 标签,而不是 input 标签
    【解决方案2】:

    您可以在Repeater的ItemDataBound事件中设置一个属性,尝试如下:

    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // if it is an item (not header or footer)
        if (e.Item.ItemType == ListItemType.Item)
        {
            // get your radioButtonList
            RadioButtonList optionsList = (RadioButtonList)e.Item.FindControl("rblOptionsList");
    
            // loop in options of the RadioButtonList
            foreach (ListItem option in optionsList.Items)
            {
                // add a custom attribute
                option.Attributes["data-flag"] = "true";
            }
        }
    }
    

    记得为你的控件设置 ID 和事件

    <asp:Repeater Id="QuestionList" ItemDataBound="QuestionList_ItemDataBound" ...>
        <ItemTemplate>
            <asp:RadioButtonList ID="rblOptionsList" DataSource='<%# Eval("Answers") %>'
                             SelectedValue='<%# Eval("SelectedAnswerId") %>'
                             DataTextField="Answer" 
                             DataValueField="AnswerId"
                             Tag='<%# Eval("QuestionId") %>' />
        </ItemTemplate>
    </asp:Repeater>
    

    【讨论】:

    • 这很有趣。我记得几年前尝试向ListItem 添加属性并且不渲染它们。我想自从我上次尝试之后事情已经发生了变化!
    • 这可行,但是它将属性应用于围绕&lt;input&gt;&lt;label&gt;&lt;span&gt; 标记。不过这对我来说很好,谢谢:)
    【解决方案3】:

    如果您需要在服务器端生成属性,最好的办法是继承RadioButtonList 控件并覆盖Render 方法。

    如果您有一个可以显示反编译代码的 Reflector 或类似产品的副本,这对于确定 ListItem 元素作为单选按钮呈现的确切位置非常有帮助。

    【讨论】:

    • 我没有反射器。你知道RadioButtonList.Render方法的默认代码是否在网上任何地方?
    • @Rachel 恐怕我不知道。我相信至少还有一个其他工具可以像 Reflector 那样工作,但我不知道它的名字。对不起。 :(
    • 好吧+1,因为它仍然是一个可行的解决方案,可能是更复杂模板的最佳选择:)
    • 谢谢你,@Rayanth!
    猜你喜欢
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多