表单提交后,Array会不断重置自己

时间:2017-04-05 03:08:21

标签: c# forms collections dynamic-programming ispostback

我有一个像这样的代码和数组:

using System;

public partial class bug : System.Web.UI.Page
{

double[] Score = new double[10];
protected void Page_Load(object sender, EventArgs e)
{

 load the form with questions from database (but show only one)
}
 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
 {
  when this clicked, evaluate the answer from TextBox1 and write the score to Score[questionnumber].
 }
 protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
 {
  go to the question of the clicked Hyperlink's number.
 }

}

所以,发生的事情是,我打开这个网站,我看到第一个问题,我把我的答案提交给它,然后它返回我的第一个问题的分数,之后我点击第二个问题的超链接,和形式带我到我的第二个问题,这是问题发生,我不知道为什么但阵列(分数数组)在这里被重置,所以当我提交第二个问题的答案时,它给出答案得分[0]而不是将其放入问号编号的索引。也许它会因此重新初始化。所以,我该怎么办才能保持不被重置?请帮助,我真的需要它。

1 个答案:

答案 0 :(得分:1)

Here is the answer I finally found:

if (!IsPostBack)
    {

        int sum = new deney().Database();
        Score = new double[sum];
        Session["myScore"] = Score;
    }

    Score = (double[])Session["myScore"];

Basically, placing the score array into the session, and getting it back every form submit. Thanks to Lasse v. Karlsen, he provided this answer to me in chat platform. I believe if chat option didn't require so much reputation, we won't see so many questions opened every day, plus Stackoverflow would be better this way I guess. A Million Thanks to everybody, especially Lasse v. Karlsen

Lasse v. Karlsen original answer on Chat Platform