如何以随机顺序对象添加到一个单选按钮列表? [英] How to add objects to a RadioButtonList based on random order ?

查看:215
本文介绍了如何以随机顺序对象添加到一个单选按钮列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建系统中的后端的无线电按钮列表。
有没有让我显示与随机顺序项目的任何方法?

我的code:

 < ASP:RadioButtonList的ID =RadioButtonList1=服务器
                                设置textAlign =右的cellpadding =10RepeatLayout =表格
                                的CausesValidation =真的CssClass =radioAnswers​​的ClientIDMode =静态>< / ASP:RadioButtonList的>

C#

  RadioButtonList1.Items.Add(新建列表项(rsQuestion(A),A))
                RadioButtonList1.Items.Add(新列表项(rsQuestion(B),B))
                RadioButtonList1.Items.Add(新建列表项(rsQuestion(C),C))
                RadioButtonList1.Items.Add(新列表项(rsQuestion(D),D))


解决方案

使用随机类来创建数字列表qith范围为1是起点和4个是单选按钮的表号。
通过号码列表创建另一个列表与您listItems中,然后循环,并将它们添加到索引中,因为这strating必须是你必须从号码列表中减一

整数

 随机RAN =新的随机();
变种号码= Enumerable.Range(1,4).OrderBy(ⅰ= GT; ran.Next())。了ToList();清单<&列表项GT; ANS =新的List<&列表项GT;();
ans.Add(新的ListItem(rsQuestion [一]的ToString(),A));
ans.Add(新的ListItem(rsQuestion [b的]的ToString(),B)。);
ans.Add(新的ListItem(rsQuestion [C]的ToString(),C));
ans.Add(新的ListItem(rsQuestion [D]的ToString(),D));的foreach(在数量INT NUM)
{
    RadioButtonList1.Items.Add(ANS [NUM - 1]);
}

I am creating a radio button list in the back end of the system. Is there any method that let me display items with random order ?

My Code :

<asp:radiobuttonlist id="RadioButtonList1" runat="server" 
                                TextAlign="Right" CellPadding="10" RepeatLayout="Table" 
                                CausesValidation="True" CssClass="radioAnswers" ClientIDMode="Static"></asp:radiobuttonlist>

c#

  RadioButtonList1.Items.Add(New ListItem(rsQuestion("a"), "A"))
                RadioButtonList1.Items.Add(New ListItem(rsQuestion("b"), "B"))
                RadioButtonList1.Items.Add(New ListItem(rsQuestion("c"), "C"))
                RadioButtonList1.Items.Add(New ListItem(rsQuestion("d"), "D"))

解决方案

Using the the random class create a list of number qith a range 1 is the starting and 4 being the table number of radio buttons. create another list with you listitems and then loop through the number list and adding them to the index, as that strating has to be a whole number you have to minus one from numbers list

Random ran = new Random();
var numbers = Enumerable.Range(1, 4).OrderBy(i => ran.Next()).ToList();

List<ListItem> ans= new List<ListItem>();
ans.Add(new ListItem(rsQuestion["a"].ToString(), "A"));
ans.Add(new ListItem(rsQuestion["b"].ToString(), "B"));
ans.Add(new ListItem(rsQuestion["c"].ToString(), "C"));
ans.Add(new ListItem(rsQuestion["d"].ToString(), "D"));

foreach (int num in numbers)
{
    RadioButtonList1.Items.Add(ans[num - 1]);
}

这篇关于如何以随机顺序对象添加到一个单选按钮列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆