c#多项选择测试

时间:2017-03-22 12:44:35

标签: c# asp.net

致力于创建基于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>

3 个答案:

答案 0 :(得分:1)

这可能会缩短评估阶段:

RadioButtonList[] lists = new RadioButtonList[] {RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };

foreach (RadioButtonList element in lists ) {
            if (element.SelectedValue == "correct") {
                Score++;
        }
}

答案 1 :(得分:1)

对于Submit_Click方法,我建议您将所有CheckBox放在List或Enumerable中。然后,您可以迭代此List并在每次迭代中评估结果。有些东西:

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);
}

答案 2 :(得分:0)

我没有确切的解决方案,但由于您询问的是指向正确的方向,以下是您应该采取的步骤:

  • 创建一个带有TextBox的用户控件,用于问题陈述和无线电控制列表或任何选项,还添加一个隐藏字段以放置标识符或正确标识问题的内容。
  • 在主网页上,您可以使用Repeater控件或使用数据动态添加用户控件。
  • 检查正确选择选择的所有代码都应该在用户控件中。
  • 在主窗体中,提交时,访问每个用户控件并收集结果。

几年前我在ASP.NET webForms中创建了一个类似的应用程序,但我有“&lt;&lt;&lt; Back”和“Next&gt;&gt;”因此,在一个页面上只显示一个问题并且整个应用程序记录了时间,因此当测试持续时间过去时,它将用户带到结束页面。