我建立了一个ASP.NET网站,进行在线考试并显示分数。我在本地发布并在IIS中托管。然后我通过lan将我的系统连接到另一个问题。我从笔记本电脑上回答了前5个问题。当我试图从另一台笔记本电脑访问该网站时,它从第5个问题而不是第一个问题开始。
public partial class MainPage : System.Web.UI.Page
{
public static int Qnum=1, Score=0,Count=0;
public static List<int> answered=new List<int>();
private SQLconnect sqlconnect = new SQLconnect();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Score"] = Score;
fnMCq();
}
}
public void fnMCq()
{
try
{
DataTable dt = new DataTable();
sqlconnect.Connection_string = System.Configuration.ConfigurationManager.AppSettings["conn_MCQ"];
string query = "select * from Questions where qid='" + Qnum + "'";
dt = sqlconnect.getDataTable(query);
string[] array = dt
.AsEnumerable()
.Select(row => row.Field<string>("question"))
.ToArray();
Question.Text = array[0];
query = "Select * from Answers where qid='" + Qnum + "'";
dt.Clear();
dt = sqlconnect.getDataTable(query);
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add(dt.Rows[0][1].ToString(), "1");
values.Add(dt.Rows[0][2].ToString(), "2");
values.Add(dt.Rows[0][3].ToString(), "3");
values.Add(dt.Rows[0][4].ToString(), "4");
Answers.DataSource = values;
Answers.DataTextField = "key";
Answers.DataValueField = "Value";
Answers.DataBind();
fnHideSubmit();
if (Qnum == 1)
btnPrevious.Enabled = false;
else
btnPrevious.Enabled = true;
if (Qnum == 20)
btnNext.Enabled = false;
else
btnNext.Enabled = true;
}
catch (Exception e)
{
Session["error"] = e.Message;
Response.Redirect("ErrorPage.aspx");
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
if (!isexamover())
{
Qnum = Qnum - 1;
fnMCq();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
if (!isexamover())
{
Qnum = Qnum + 1;
fnMCq();
}
}
public void fnUpdateScore()
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn_MCQ"]);
int selectedanswer,answer;
selectedanswer=Convert.ToInt32(Answers.SelectedValue);
string query = "select answer from questions where qid='"+Qnum+"'";
SqlCommand cmd=new SqlCommand(query,conn);
conn.Open();
sqlconnect.Connection_string = System.Configuration.ConfigurationManager.AppSettings["conn_MCQ"];
answer =Convert.ToInt32( cmd.ExecuteScalar().ToString()) ;
if (answer == selectedanswer)
{
Score = Score + 1;
Session["Score"] = Score;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
answered.Add(Qnum);
fnHideSubmit();
fnUpdateScore();
if (!qbound())
{
if (!isexamover())
{
++Qnum;
fnMCq();
}
else
{
Session["Score"] = Score;
Response.Redirect("ScoreDisplay.aspx");
}
}
}
public void fnHideSubmit()
{
foreach (int i in answered)
{
if (i == Qnum)
{
btnSubmit.Enabled = false;
break;
}
else
btnSubmit.Enabled = true;
}
}
public bool qbound()
{
if (Qnum == 1 || Qnum == 20)
return true;
else
return false;
}
public bool isexamover()
{
if (answered.Count == 20)
{
Session["Score"] = Score;
Response.Redirect("ScoreDisplay.aspx");
return true;
}
return false;
}
答案 0 :(得分:1)
您遇到的问题是将所有内容存储在静态字段中:
public static int Qnum=1, Score=0,Count=0;
public static List<int> answered=new List<int>();
这意味着这些字段只有一个实例。这些字段与类绑定,而不是该类的特定实例。
为了解决这个问题,您可以将它们存储在Session
中,这样您就可以记住单个会话中多个页面请求的值。
答案 1 :(得分:0)
在变量上使用static时,.NET会在内存中为该变量保留一个点。我想当新用户尝试在其他人进行测验时,它会引用具有另一个用户计数的相同变量位置。
public int Qnum=1, Score=0,Count=0;
public List<int> answered=new List<int>();