Array keeps resetting itself after form submit

99 views Asked by At

I have a code and an array inside like this one :

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

}

So, what happens is, I open this website, I see there first question, I put my answer and submit it, then it returns the score of my first question, after that I click 2nd question's hyperlink, and form takes me to my 2nd question, here is the problem happens, I don't know why but the array (Score array) gets resetted here, so when I submit my answer for 2nd question, it puts the answer to Score[0] instead of putting it to the index of the number of question number. maybe it re-initialize again because of that. So, What should I do to keep not happening it being resetted? Please help, I really need it.

1

There are 1 answers

1
Cem Sultan On BEST ANSWER

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