【问题标题】:Calculating score on a quiz test using asp.net C# with database使用带有数据库的 asp.net C# 计算测验测试的分数
【发布时间】:2021-08-17 07:52:10
【问题描述】:

我正在尝试使用数据库在 asp.net C# 中的 Web 应用程序中创建一个测验。测验有 25 个问题和 5 个答案,每个答案都有一定的分数(完全同意(1),同意(2),不确定(3),不同意(4),完全不同意(5))。

问题和答案都在数据库中。 我想要的是当我单击提交按钮以计算这些问题的分数并将分数从我的数据库中放入一个表中时。 我尝试做一个 if (radiobutton1.checked){ 分数=2} 但它不起作用,因为中继器......我想我不确定。 我尝试删除转发器,但是当我这样做时,问题和答案不会显示在网页上。

在我的 aspx 文件中,我编写代码来显示来自数据库的数据(使用中继器和表格):

  <asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
    <table>
        <tr>
            <td> <%#Eval("IDq") %> )  <%#Eval("qustion") %></td></tr>
        <tr>
            <td>
                <asp:RadioButton ID="RadioButton1" runat="server" Text='<%#Eval("ans1")%>' GroupName="quiz" Value="1"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton2" runat="server" Text='<%#Eval("ans2") %>' GroupName="quiz" Value="2"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton3" runat="server" Text='<%#Eval("an3") %>' GroupName="quiz" Value="3"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton4" runat="server" Text='<%#Eval("ans4") %>' GroupName="quiz" Value="4"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton5" runat="server" Text='<%#Eval("ans5") %>' GroupName="quiz" Value="5"></asp:RadioButton>
                <br />
            </td>
        </tr> </table>
</ItemTemplate></asp:Repeater>

【问题讨论】:

    标签: c# asp.net web-applications


    【解决方案1】:

    好的,你有一个好的开始。

    我建议用它代替 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,然后您保存测试的总结果(或正确计数,或错误计数)。

    所以,最终的结果是这样的:

    【讨论】:

    • 非常感谢。但我的测验没有错误或正确答案。只是回答每个特定数量的点。 (例如问题 1 (ans1 [1p] , ans 2[2p]..ans5[5p] 等等所有问题。当点击提交按钮时,它必须总结所有问题的分数。例如问题 1 - 答案 2 被选择(有 2p)和问题 2- 答案 1 被选择(有 1p)按钮应该做 2+1 最终结果 3 p
    • 你知道我该怎么做吗? :)
    • 您需要上面表格中显示的 5 个问题(文本)。我们有一栏是正确答案。您只需要多一列获得正确答案的分数。如果您为错误的答案奖励分数,那么对于每个可能的 5 个问题,您需要为每个分数增加 5 个额外的列。如果只有一个正确答案,则只需要 1 个额外的列来保存正确答案所获得的分数。上面的代码显示了我们如何汇总正确和错误的答案。该代码很容易更改为使用问题点列为正确答案加分。
    • 给出你没有错误或正确的答案?然后你需要 5 个额外的列 Points1、Points2、points3..points5。因此,您需要在数据库中为每个问题提供所需的点数。 (除非它们都是 ans1 的 1 分,ans2 的 2 分等。但是如果每个问题要获得的分数是某个数字,那么只需添加 5 个额外的列来保存每个答案的分数。然后你可以使用贴近发布的确切代码以汇总给出的答案 - 代替像代码那样添加 +1,只需为给出的答案添加 Points1-Point5 列值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2018-06-12
    相关资源
    最近更新 更多