1

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

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.
 }

}

所以,发生的事情是,我打开这个网站,我看到第一个问题,我输入我的答案并提交它,然后它返回我第一个问题的分数,然后我点击第二个问题的超链接,表单将我带到我的第二个问题问题,这里是问题发生了,我不知道为什么,但是数组(分数数组)在这里被重置,所以当我提交我的第二个问题的答案时,它把答案放到 Score[0] 而不是把它放到问题编号的索引。也许它因此再次重新初始化。那么,我应该怎么做才能不发生它被重置?请帮忙,我真的需要它。

4

1 回答 1

1

这是我最终找到的答案:

if (!IsPostBack)
    {

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

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

基本上,将分数数组放入会话中,并在每次提交表单时将其取回。感谢 Lasse v. Karlsen,他在聊天平台上为我提供了这个答案。我相信如果聊天选项不需要这么高的声誉,我们不会每天看到这么多问题被打开,而且我猜 Stackoverflow 会更好。一百万感谢大家,尤其是 Lasse v. Karlsen

Lasse v. Karlsen 在聊天平台上的原始答案

于 2017-04-10T03:15:01.690 回答