Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将自定义属性添加到RadioButtonList项?_C#_Asp.net_Html_Radiobuttonlist - Fatal编程技术网

C# 如何将自定义属性添加到RadioButtonList项?

C# 如何将自定义属性添加到RadioButtonList项?,c#,asp.net,html,radiobuttonlist,C#,Asp.net,Html,Radiobuttonlist,如何将绑定添加到使用绑定的RadioButtonList生成的项目 我的代码如下所示: <asp:Repeater Id="QuestionList" ...> <ItemTemplate> <asp:RadioButtonList DataSource='<%# Eval("Answers") %>' SelectedValue='<%# Eval("SelectedAns

如何将绑定添加到使用绑定的
RadioButtonList
生成的项目

我的代码如下所示:

<asp:Repeater Id="QuestionList" ...>
    <ItemTemplate>
        <asp:RadioButtonList DataSource='<%# Eval("Answers") %>'
                         SelectedValue='<%# Eval("SelectedAnswerId") %>'
                         DataTextField="Answer" 
                         DataValueField="AnswerId"
                         Tag='<%# Eval("QuestionId") %>' />
    </ItemTemplate>
</asp:Repeater>
public class Question
{
    int QuestionId;
    string Question;
    List<Answer> Answers;
}

public class Answers
{
    int AnswerId;
    string Answer;
    bool SomeFlag;
}
<input type="radio" data-flag="true" ... />

有没有一种方法可以将html数据属性添加到从绑定的
RadioButtonList
生成的输入对象中?

如果需要生成属性,服务器端的最佳选择是将
RadioButtonList
控件子类化,并覆盖
Render
方法


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

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

在服务器端

为单选按钮列表生成的html


您可以在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>


很有趣,这以前不起作用。我想我还没有在几个版本中尝试过+1指出如何从jQuery访问它:)我花了一分钟才注意到属性被添加到span标记,而不是输入标记,这非常有趣。我记得几年前,我尝试向
列表项添加属性,但没有呈现它们。我想自从上次我试过以后,情况已经改变了!这是可行的,但是它将属性应用于围绕
标记。不过我没问题,谢谢:)我没有反光镜。你知道
RadioButtonList.Render
方法的默认代码是否在线吗?@Rachel我恐怕不知道。我相信至少还有一种工具可以像Reflector那样工作,但我现在还不知道它的名字。对不起(无论如何+1,因为它仍然是一个可行的解决方案,对于更复杂的模板来说可能是最好的:)9年太晚了,但是谢谢你,@Rayanth!
$(':radio[id*=RadioButtonList1]').click(function(){
      alert($(this).closest('span').data('flag'));
})
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";
        }
    }
}
<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>