如何计算c#中在线考试的分数?

时间:2016-05-03 12:27:59

标签: c# webforms

在按钮的onclick事件中,我从aspx页面获取控件,并且我还使用了if语句以查看哪个单选按钮处于活动状态并且已在varialbe中存储相应的值{{1使用此selans,我会将其与隐藏字段的值进行比较,以便查找选中的单选按钮是否为正确答案,如果答案是正确的,即selans中的值与隐藏字段中的值匹配(实际答案)和变量“count”(最初用值0初始化)相应地增加,并且所有这些代码都放在“for循环”中,这将执行到no。 GridView中的控件(您可以将其与问题编号联系起来,就像GridView生成新控件的每个记录一样)。

我使用了变量

1)selans: - 从gridview1获得总分

2)totalgrid1: - 从gridview2获得总分

3)totalgrid2: - total + totalgrid1 ...这是考试的总分

4)totalgrid2: - 从gridview1

获取正确答案的数量

5)correct1: - 从gridview2获取正确答案的数量

6)correct2: - correct + correct1 .....用户正确答案总数

在运行此程序后,即尝试正确答案我得到“得分= 6”而不是在结果页面上得到4,因为只有4个问题,每个问题带有1个标记,那么我怎样才能获得6分?和 我也得到了“正确答案的数量=没有显示任何内容(它是空白的)”......它应该显示一些价值。

我无法找出我犯错误的地方。下面是我的代码。看看我的代码。告诉我我犯错的地方和解决方案

correct2

Result.aspx.cs: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{



    if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
        GridView1.DataBind();


        GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
        GridView2.DataBind();

    }
}

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}


        protected void btn_Click(object sender, EventArgs e)
      {

            RadioButton r1, r2, r3, r4;
            HiddenField hdn;
            int count = 0;
            int neg = 0;
            int total=0;
            int totalgrid1=0;
            int totalgrid2=0;

            int attempt1 = 0;
            int attempt2 = 0;
            int Tattempt = 0;
            int correct = 0;
            int correct1 = 0;
            int correct2 = 0;

            string selans = "-1";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
                r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
                r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
                r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
                hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
                if (r1.Checked)
                {
                    selans = r1.Text;

                }
                else if (r2.Checked)
                {
                    selans = r2.Text;

                }
                else if (r3.Checked)
                {
                    selans = r3.Text;

                }
                else if (r4.Checked)
                {
                    selans = r4.Text;

                }



                if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
                {
                   attempt1++;

                   if (hdn.Value == selans)
                   {
                      count++;
                       correct1++;
                   }
                    else
                    {
                      neg--;
                    }
                }

                totalgrid1 = count + neg;


            }

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
        r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
        r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
        r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
        hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
        if (r1.Checked)
        {
            selans = r1.Text;

        }
        else if (r2.Checked)
        {
            selans = r2.Text;

        }
        else if (r3.Checked)
        {
            selans = r3.Text;

        }
        else if (r4.Checked)
        {
            selans = r4.Text;

        }



        if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
        {
            attempt2++;

            if (hdn.Value == selans)
            {
                count++;
                correct2++;
            }
            else
            {
                neg--;
            }
        }

        totalgrid2 = count + neg;

    }
    total = totalgrid1 + totalgrid2;
    Tattempt = attempt1 + attempt2;
    correct = correct1 + correct2;
    Label2.Text = total.ToString();
    Label3.Text = Tattempt.ToString();
    Label4.Text = correct.ToString();

    Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns" +Label4.Text);

}


}

1 个答案:

答案 0 :(得分:1)

我通过在按钮的onclick事件上设置断点来了解我犯错误的地方。

以下是解决方案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
        GridView1.DataBind();


        GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
        GridView2.DataBind();

    }
}

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}


        protected void btn_Click(object sender, EventArgs e)
      {

            RadioButton r1, r2, r3, r4;
            HiddenField hdn;
            int count1 = 0;
            int count2 = 0;
            int neg1 = 0;
            int neg2 = 0;
            int total=0;
            int totalgrid1=0;
            int totalgrid2=0;

            int attempt1 = 0;
            int attempt2 = 0;
            int Tattempt = 0;
            int correct = 0;
            int correct1 = 0;
            int correct2 = 0;

            string selans = "-1";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
                r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
                r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
                r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
                hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
                if (r1.Checked)
                {
                    selans = r1.Text;

                }
                else if (r2.Checked)
                {
                    selans = r2.Text;

                }
                else if (r3.Checked)
                {
                    selans = r3.Text;

                }
                else if (r4.Checked)
                {
                    selans = r4.Text;

                }



                if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
                {
                   attempt1++;

                   if (hdn.Value == selans)
                   {
                      count1++;
                       correct1++;
                   }
                    else
                    {
                      neg1--;
                    }
                }

                totalgrid1 = count1 + neg1;


            }

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
        r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
        r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
        r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
        hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
        if (r1.Checked)
        {
            selans = r1.Text;

        }
        else if (r2.Checked)
        {
            selans = r2.Text;

        }
        else if (r3.Checked)
        {
            selans = r3.Text;

        }
        else if (r4.Checked)
        {
            selans = r4.Text;

        }



        if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
        {
            attempt2++;

            if (hdn.Value == selans)
            {
                count2++;
                correct2++;
            }
            else
            {
                neg2--;
            }
        }

        totalgrid2 = count2 + neg2;

    }
    total = totalgrid1 + totalgrid2;
    Tattempt = attempt1 + attempt2;
    correct = correct1 + correct2;
    Label2.Text = total.ToString();
    Label3.Text = Tattempt.ToString();
    Label4.Text = correct.ToString();

    Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns=" +Label4.Text);

}


}