gpt4 book ai didi

c# - 当多个用户同时参加考试时,在线考试考试会分开

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:07 25 4
gpt4 key购买 nike

当多个用户参加测试时,我在 asp.net 中的在线考试申请测试被拆分。我的 20 个问题测试被分成 10 个 10 个问题给同时参加测试的每个用户。我在我的应用程序中使用 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;
}

}
}

最佳答案

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

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

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

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

关于c# - 当多个用户同时参加考试时,在线考试考试会分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43040976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com