我正在创建一个基于web的修订资源,我正在尝试用asp.net在c#中创建一个多项选择测验。我已经创建了它并让它工作,但它很长,想知道是否有人知道我如何使代码更紧凑,也当它打印最终分数时,它出现在我网站上的导航栏上方,我不知道为什么。
此外,答案可以更改,提交按钮只是再次点击,我找不到任何关于如何停止这一点的爱。
我对任何事情都非常陌生,并且正在尽我最大的努力。如果有人能给我指出正确的方向,或者提出一些可能有帮助的建议,那就太好了。
谢谢
我对代码的长度表示歉意:
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="server">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList2" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList3" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList4" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList5" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList6" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList7" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList8" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList9" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList10" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Submit Final Answers" OnClick="Submit_Click" />
<script runat="server">
protected void Submit_Click(object sender, EventArgs e)
{
int Score = 0;
string selectedValue1 = RadioButtonList1.SelectedValue;
if (selectedValue1 == "Correct")
{
Score++;
}
string selectedValue2 = RadioButtonList2.SelectedValue;
if (selectedValue2 == "Correct")
{
Score++;
}
string selectedValue3 = RadioButtonList3.SelectedValue;
if (selectedValue3 == "Correct")
{
Score++;
}
string selectedValue4 = RadioButtonList4.SelectedValue;
if (selectedValue4 == "Correct")
{
Score++;
}
string selectedValue5 = RadioButtonList5.SelectedValue;
if (selectedValue5 == "Correct")
{
Score++;
}
string selectedValue6 = RadioButtonList6.SelectedValue;
if (selectedValue6 == "Correct")
{
Score++;
}
string selectedValue7 = RadioButtonList7.SelectedValue;
if (selectedValue7 == "Correct")
{
Score++;
}
string selectedValue8 = RadioButtonList8.SelectedValue;
if (selectedValue8 == "Correct")
{
Score++;
}
string selectedValue9 = RadioButtonList9.SelectedValue;
if (selectedValue9 == "Correct")
{
Score++;
}
string selectedValue10 = RadioButtonList10.SelectedValue;
if (selectedValue10 == "Correct")
{
Score++;
}
Response.Write(Score);
}
</script>
</asp:Content>发布于 2017-03-22 20:58:53
这可能会缩短评估阶段:
RadioButtonList[] lists = new RadioButtonList[] {RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };
foreach (RadioButtonList element in lists ) {
if (element.SelectedValue == "correct") {
Score++;
}
}发布于 2017-03-22 21:01:48
对于Submit_Click方法,我建议您将所有的CheckBoxes放在一个列表中或某种类型的Enumerable中。然后,您可以对此列表进行迭代,并在每次迭代中评估结果。某物类似于:
protected void Submit_Click(object sender, EventArgs e)
{
int score = 0;
List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, ... RadioButtonList3 };
foreach (var element in list)
{
if (element.SelectedValue == "correct") {
score++;
}
Response.Write(score);
}发布于 2017-03-22 22:31:20
我没有一个确切的解决方案,但既然你问的是如何被指引到正确的方向,下面是你应该采取的步骤:
几年前,我在ASP.NET webForms中做了一个类似的应用,但我有"<< Back“和"Next >>”按钮,所以在一个页面上只显示一个问题,整个应用记录时间,所以当测试持续时间过去时,它会将用户带到结束页面。
https://stackoverflow.com/questions/42952007
复制相似问题