C# 以随机顺序向RadioButtonList添加数据

C# 以随机顺序向RadioButtonList添加数据,c#,asp.net,random,radiobuttonlist,C#,Asp.net,Random,Radiobuttonlist,我正在尝试以如下所示的随机顺序将数据添加到RadioButtonList(在btnGetQuestion中单击),但是,在回发btnCheck中单击RadioButtonList中的选定项会更改为列表中的其他项。为什么会发生这种情况,以及如何避免这种情况的任何建议 .aspx: <form id="form1" runat="server"> <div> <asp:Button ID="btnGetQuestion" runat="server" Text=

我正在尝试以如下所示的随机顺序将数据添加到RadioButtonList(在btnGetQuestion中单击),但是,在回发btnCheck中单击RadioButtonList中的选定项会更改为列表中的其他项。为什么会发生这种情况,以及如何避免这种情况的任何建议

.aspx:

<form id="form1" runat="server">
<div>
    <asp:Button ID="btnGetQuestion" runat="server" Text="Get Question" OnClick="btnGetQuestion_Click" />
    <asp:Label ID="lblQuestion" runat="server" Text=""></asp:Label>
    <asp:RadioButtonList ID="rblQuestions" runat="server"></asp:RadioButtonList>
    <asp:Button ID="btnCheck" runat="server" Text="Check Answer" OnClick="btnCheck_Click" />
    <asp:Label ID="lblAnswer" runat="server" Text=""></asp:Label>
    <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
</div>
</form>
c:


在将列表项添加到列表的代码中,您正在添加具有相同值“n”的多个列表项。如果选择一个值为“n”的项目,回发后将选择一个值为“n”的项目可能是第一个。如果需要保留正确的选择,则需要将不同的值绑定到每个复选框。 我认为您必须将选项和值对保持在单独的视图状态变量中,绑定选项1、选项2。。。作为单选按钮列表的值,并在进一步处理过程中从viewstate变量中获取适当的值

ans.Add(new ListItem("option 1", "y"));
ans.Add(new ListItem("option 2", "n"));
ans.Add(new ListItem("option 3", "n"));
ans.Add(new ListItem("option 4", "n"));
ans.Add(new ListItem("option 5", "n"));

你能把你的页面上传到加载处理程序吗?谢谢。使用不同的值,而不是多个“n”值。然而,我不理解你从“我认为你必须将选项和值对保持在单独的视图状态变量中”开始的句子中的意思。我的意思是,如果你将选项1、选项2等绑定到值,那么你需要知道每个选项的实际值y/n是什么。为了跟踪这一点,您可以将列表存储在viewstate中,并从中获取值。我修改了“n”值,使它们不同。但是,为什么下面的代码不起作用:protectedvoidbtnclear\u clickobjectsender,EventArgs e{rblQuestions.Items.Clear;}
ans.Add(new ListItem("option 1", "y"));
ans.Add(new ListItem("option 2", "n"));
ans.Add(new ListItem("option 3", "n"));
ans.Add(new ListItem("option 4", "n"));
ans.Add(new ListItem("option 5", "n"));