当多个用户同时进行测试时,在线考试测试会分裂

时间:2017-03-27 08:05:34

标签: c# asp.net

当多个用户参加测试时,我在asp.net中的在线考试应用程序测试会被拆分。我的20个问题测试被分成10个10个问题给每个正在同时参加测试的用户。我在我的appliaction中使用session是代码请帮助。

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

namespace OnlineExamDesign
{
    public partial class Exam : System.Web.UI.Page
    {
        static int index = 0;
        static int selection = 0;
        static int QuestionNums = 0;
        clsExam examObj = new clsExam();
        DataSet ds; DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                index = 0;
                selection = 0;
                string set = Session["CandidateSet"].ToString();
                ds = examObj.getQuestion(set);
                Session["Ques"] = ds;
                displayQuestion(index);
                btnPrevious.Enabled = false;
                if (Session["candidateName"] != null)
                    lblCandidateName.Text = Session["candidateName"].ToString();
                clearResultData();
            }


        }

        private void clearRadioButtons()
        {
            rbtnOption1.Checked = false;
            rbtnOption2.Checked = false;
            rbtnOption3.Checked = false;
            rbtnOption4.Checked = false;
        }

        private void clearResultData()
        {
            for (int n = 0; n < clsExam.AnsList.Length; n++)
            {
                clsExam.AnsList[n] = 0;
            }

        }

        private void saveCandidateData()
        {
            Candidate obj = new Candidate();
            obj.Fname = Session["CandidateFName"].ToString();
            obj.Lname = Session["CandidateLName"].ToString();
            obj.Contact = Session["CandidateContact"].ToString();
            obj.Domain = Session["CandidateDomain"].ToString();
            obj.Team = Session["CandidateTeam"].ToString();
            obj.Set = Session["CandidateSet"].ToString();
            obj.ExamStartTime = (DateTime)Session["strttime"];
            obj.ExamEndTime = System.DateTime.Now;
            obj.Score = examObj.score;
            obj.actionCandidateDetails("save");
            Session["Score"] = obj.Score;

        }

        private int displayQuestion(int index)
        {
            ds = (DataSet)Session["Ques"];
            dt = ds.Tables[0];
            int num = dt.Rows.Count;

            if (index <= num)
            {
                lblQuestionNum.Text = Convert.ToString((index+1));

                lblQuestion.Text = dt.Rows[index]["questions_question_nvc"].ToString();
                rbtnOption1.Text = dt.Rows[index]["questions_option1_nvc"].ToString();
                rbtnOption2.Text = dt.Rows[index]["questions_option2_nvc"].ToString();
                rbtnOption3.Text = dt.Rows[index]["questions_option3_nvc"].ToString();
                rbtnOption4.Text = dt.Rows[index]["questions_option4_nvc"].ToString();
            }
            else if (index > num - 1)
                btnNext.Enabled = false;
            else if (index <= 0)
                btnPrevious.Enabled = false;

            return num;
        }

        private void retainChoice()
        {
            if ((clsExam.AnsList[index]).Equals(1))
            {
                rbtnOption1.Checked = true;
            }
            else if ((clsExam.AnsList[index]).Equals(2))
            {
                rbtnOption2.Checked = true;
            }
            else if ((clsExam.AnsList[index]).Equals(3))
            {
                rbtnOption3.Checked = true;
            }
            else if ((clsExam.AnsList[index]).Equals(4))
            {
                rbtnOption4.Checked = true;
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {

            btnPrevious.Enabled = true;
            rbtnOptionCheckedChanged(sender, e);
            examObj.storeAns(index, selection);
            selection = 0;
            clearRadioButtons();
            index++;
            int QuestionNums = displayQuestion(index);
            retainChoice();
            if (index >= QuestionNums - 1)
                btnNext.Enabled = false;
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (index > 0)
            {
                rbtnOptionCheckedChanged(sender, e);
                examObj.storeAns(index, selection);
                selection = 0;
                clearRadioButtons();
                index--;
                displayQuestion(index);
                retainChoice();
                btnNext.Enabled = true;
            }
            else if (index <= 0)
            {
                btnPrevious.Enabled = false;
            }
            else if (index < QuestionNums - 1)
            {
                btnNext.Enabled = true;
            }
            else
            {
                btnPrevious.Enabled = false;
                btnNext.Enabled = true;
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            int i = 0;
            examObj.storeAns(index, selection);

            ds = (DataSet)Session["Ques"];
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["questions_answer_nvc"].Equals(clsExam.AnsList[i]))
                {

                    examObj.countScore();
                    i++;
                }
            }
            Response.Write(examObj.score);
            saveCandidateData();
            index = 0;
            clearResultData();
            Response.Redirect("ThankYou.aspx");

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int i = 0;
            examObj.storeAns(index, selection);

            ds = (DataSet)Session["Ques"];
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["questions_answer_nvc"].Equals(clsExam.AnsList[i]))
                {

                    examObj.countScore();
                    i++;
                }
            }
            Response.Write(examObj.score);
            saveCandidateData();
            index = 0;
            clearResultData();
            Response.Redirect("ThankYou.aspx");

        }

        protected void rbtnOptionCheckedChanged(object sender, EventArgs e)
        {
            if (rbtnOption1.Checked)
                selection = 1;
            else if (rbtnOption2.Checked)
                selection = 2;
            else if (rbtnOption3.Checked)
                selection = 3;
            else if (rbtnOption4.Checked)
                selection = 4;
        }

    }
}

1 个答案:

答案 0 :(得分:3)

我将重复我在公司中出名的句子: 静态是邪恶的!

是的,确实如此。 Web应用程序中的static表示它在所有用户之间共享,这意味着用户1的index与用户2的index相同。

您可以通过将静态变量存储在会话对象中来轻松解决此问题。我只是把它隐藏在一个属性中:

public int Index
{
    get
    {
        return (Session["Exam_Index"] as int?) ?? 0;
    }
    set
    {
        Session["Exam_Index"] = value;
    }
}