我想以随机顺序从数据库中填充5个值的RadioButtonList。
所以我尝试将5个值存储在Session对象中,然后使用以下代码将值随机添加到RadioButtonList(rblQuestions):
Random ran = new Random();
var numbers = Enumerable.Range(1, 5).OrderBy(i => ran.Next()).ToList();
List<ListItem> ans = new List<ListItem>();
ans.Add(new ListItem(Session["Value1"].ToString(), "y"));
ans.Add(new ListItem(Session["Value2"].ToString(), "n"));
ans.Add(new ListItem(Session["Value3"].ToString(), "n"));
ans.Add(new ListItem(Session["Value4"].ToString(), "n"));
ans.Add(new ListItem(Session["Value5"].ToString(), "n"));
foreach (int num in numbers)
{
rblQuestions.Items.Add(ans[num - 1]);
}
这很有效。但是,当我单击同一表单上的另一个按钮时,列表中的所选项目会随机更改。为什么这样,我怎么能避免这个?
[编辑以包含代码。]
<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#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
public partial class questions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGetQuestion_Click(object sender, EventArgs e)
{
getRandomQuestion();
}
protected void btnCheck_Click(object sender, EventArgs e)
{
}
protected void getRandomQuestion()
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlDataReader reader;
try
{
conn.Open();
string cmdText = "SELECT * FROM questions ORDER BY RAND() LIMIT 1";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
reader = cmd.ExecuteReader();
if (reader.Read())
{
lblQuestion.Text = reader["question"].ToString();
Session["Answer1"] = reader["answer1"].ToString();
Session["Answer2"] = reader["answer2"].ToString();
Session["Answer3"] = reader["answer3"].ToString();
Session["Answer4"] = reader["answer4"].ToString();
Session["Answer5"] = reader["answer5"].ToString();
}
else
{
lblError.Text = "(no questions found)";
}
reader.Close();
}
catch
{
lblError.Text = "Database connection error - failed to insert record.";
}
finally
{
conn.Close();
}
Random ran = new Random();
var numbers = Enumerable.Range(1, 5).OrderBy(i => ran.Next()).ToList();
List<ListItem> ans = new List<ListItem>();
ans.Add(new ListItem(Session["Answer1"].ToString(), "y"));
ans.Add(new ListItem(Session["Answer2"].ToString(), "n"));
ans.Add(new ListItem(Session["Answer3"].ToString(), "n"));
ans.Add(new ListItem(Session["Answer4"].ToString(), "n"));
ans.Add(new ListItem(Session["Answer5"].ToString(), "n"));
foreach (int num in numbers)
{
rblQuestions.Items.Add(ans[num - 1]);
}
}
}