好的,你有一个好的开始。
我建议用它代替 RadioGroup 标签?
这只会让你自动选择一个。问题是我们仍然必须检查所有 5 个控件。
在这种情况下更好地控制使用所谓的 RadioButtonList。它的工作方式与使用 RadioGroup 非常相似,但真的很不错吗?
为ONE控件,如果单选按钮组有3个或15个选项,则作为ONE控件返回
the index of the selection (0 to N)
the text value of the selection
the value value of the selection.
所以我建议使用 RadioButton 列表 - 因为它是返回选择的“一件事”。
RadioButtonList 的“主要”问题是您不能像现在使用的那样使用那些很酷的数据绑定表达式(这对您来说是一个很好的调用/设计)。
但是,我们要么编写一个循环来“获取/检查” 5 个 raido 按钮,要么编写一个循环来填充 Radiobutton 列表 - 我认为使用代码来填充 RadioButton 列表是更好的选择。
另外,你是新手 - 我看到你正在尝试使用一些“表格和”来帮助你布置东西。你真的不需要这个。
现在,因为上述所有想法都为我们节省了大量时间和代码?好吧,那我们可以添加额外的代码来说
show the score and results after we submit
tell the user they did NOT answer a question - they MUST!!!
heck, toss is a cool check box, or X box to show wrong or right.
Allow a question to only have say 2 answers - not always all 5
好的,首先,我们的桌子 - 它看起来像这样:
所以,我们有 IDq(问题 ID)。 1 到 5 个可能的答案,然后是正确答案的列。
好的,现在我们的标记。正如我所说,由于我们减少了如此多的标记和代码,所以我添加了当用户没有回答所有问题时出现的红色粗体消息。
我还添加了一个“结果”框,显示您点击提交的时间(编号错误,正确)。
所以,现在我们的标记变成了这样:
<div style="width:35%">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div style="float:left">
<asp:Label ID="Question" runat="server"
Text = '<%# Eval("IDq").ToString + " ) " + Eval("question") %>'
Font-Size="X-Large">
</asp:Label>
</div>
<div style="float:right">
<img id="MyImage" runat="server" src="" height="48" width="48" />
</div>
<div style="clear:both">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" ></asp:RadioButtonList>
</div>
<br />
<hr></hr>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="cmdDone" runat="server" Text="Submit" />
<div id="MyError" style="display:none;normal;color:red" runat="server">
<h2>You have not answered all questions</h2>
<h2>Please answer all questions before submitting</h2>
</div>
<div id="Results" runat="server" style="clear:both; display:none" >
<h2>Results</h2>
<asp:TextBox ID="MyCorrect" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="InCorrect" runat="server"></asp:TextBox>
</div>
</div>
所以,没有太多标记 - 我们添加了很多新功能。
我还提出了显示正确和错误复选框的想法 - 您可以删除该部分 - 但它确实可以帮助您在这里学到很多东西。
所以,现在当我运行代码时,我看到了:
所以请注意我的问题是正确的。正如我所说 - 你可能想删除它。
并注意我尚未回答所有问题的消息。
所以,如前所述,我们需要多一点代码来加载中继器,但之后要检查的代码就少了。
所以,我们的代码现在看起来像这样:
private DataTable MyTable = new DataTable();
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack == false)
{
LoadData();
ViewState["MyTable"] = MyTable;
}
else
MyTable = ViewState["MyTable"];
}
public void LoadData()
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from tblQuestions ORDER BY IDq",
new SqlConnection(My.Settings.TEST4)))
{
cmdSQL.Connection.Open();
MyTable.Load(cmdSQL.ExecuteReader);
Repeater1.DataSource = MyTable;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRow MyRow = MyTable.Rows(e.Item.ItemIndex);
RadioButtonList rBtnList = e.Item.FindControl("RadioButtonList1");
for (var i = 1; i <= 5; i++)
{
var strR = "ans" + i;
if (IsDBNull(MyRow(strR)) == false)
{
ListItem nItem = new ListItem(MyRow(strR), i);
rBtnList.Items.Add(nItem);
}
}
}
}
protected void cmdDone_Click(object sender, EventArgs e)
{
// check all rows - make sure all have a answer
// show check box for correct answers
// total up correct answers (count)
// total up wrong answers (count)
MyError.Style("Display") = "none";
int Correct = 0;
int Wrong = 0;
int NotAnser = 0;
foreach (RepeaterItem ritem in Repeater1.Items)
{
RadioButtonList RadioBut = ritem.FindControl("RadioButtonList1");
if (RadioBut.SelectedIndex >= 0)
{
HtmlImage MyImage = ritem.FindControl("MyImage");
if (MyTable.Rows(ritem.ItemIndex).Item("Answer") == RadioBut.SelectedIndex + 1)
{
Correct += 1;
MyImage.Src = "/Content/ok.png";
}
else
{
Wrong += 1;
MyImage.Src = "/Content/reject.png";
}
}
else
{
NotAnser += 1;
}
}
// if missed questions then display warning.
if (NotAnser > 0)
MyError.Style("Display") = "normal";
else
{
MyCorrect.Text = "Correct answers = " + Correct;
InCorrect.Text = "Wrong answers = " + Wrong;
Results.Style("Display") = "normal";
}
}
因此您可以在提交按钮代码中看到,我们现在可以轻松循环转发器,获取用户答案,检查表格数据。
并注意另一个技巧 - 我将 MyTable 持久保存到 ViewState 中。我这样做是因为我不想每次都重新加载表格 - 这只是意味着任何按钮点击、控制和代码背后?数据表总是触手可及
现在,鉴于上面确实汇总了答案,那么您将不得不再添加 2-5 行代码来写出总数或结果 - 目前尚不清楚您是否打算在其中使用其他表格说出一些学生 ID 或用户 ID,然后您保存测试的总结果(或正确计数,或错误计数)。
所以,最终的结果是这样的: