-2

我建立了一个 ASP.NET 网站,该网站进行在线考试并显示分数。我在本地发布并托管在 IIS 中。然后我通过局域网连接了我的系统与另一个问题。我用笔记本电脑回答了前 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;

    }
4

2 回答 2

1

您遇到的问题来自将所有内容存储在静态字段中:

public static int Qnum=1, Score=0,Count=0;
public static List<int> answered=new List<int>();

这意味着这些字段只有一个实例。这些字段与类相关,而不是该类的特定实例。

为了解决这个问题,您可以将这些存储在 中Session,这将允许您记住单个会话中多个页面请求的值。

于 2013-09-06T15:59:39.527 回答
0

在变量上使用静态时,.NET 将在内存中为该变量保留一个位置。我在想,当一个新用户尝试在其他人参加测验时,它将引用具有其他用户计数的相同变量位置。

public int Qnum=1, Score=0,Count=0;
public List<int> answered=new List<int>();
于 2013-09-06T15:59:20.467 回答