首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多人同时考试时在线考试拆分

多人同时考试时在线考试拆分
EN

Stack Overflow用户
提问于 2017-03-27 16:05:34
回答 1查看 42关注 0票数 1

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

代码语言:javascript
复制
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;
        }

    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-27 16:07:19

我要重复一句我在我们公司很出名的话:静态是邪恶的!

是的,就是这样。web应用程序中的static意味着它在所有用户之间共享,这意味着用户1的index与用户2的index相同。

通过将静态变量存储在会话对象中,可以很容易地解决这个问题。我会简单地将其隐藏在一个属性中:

代码语言:javascript
复制
public int Index
{
    get
    {
        return (Session["Exam_Index"] as int?) ?? 0;
    }
    set
    {
        Session["Exam_Index"] = value;
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43040976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档