0

I am having problems getting my page to maintain state. View state is enabled by default but everytime I click a button it resets the form. This is the code I have

 protected void Page_Load(object sender, EventArgs e)
    {


        Levels loadGame = new Levels(currentGame);

        int [] gameNums =  loadGame.getLevelNums();
        int inc = 1;
        foreach(int i in gameNums){

            if (i != 0)
            {
                TextBox tb = (TextBox)FindControl("TextBox" + inc);
                tb.Text = i.ToString();
                tb.Enabled = false;
            }
            else {
                //leave blank and move to next box
            }

            inc++;
        }

This is the initial load

protected void NormalButton_Click(object sender, EventArgs e)
    {

        clearBoxes();//clear boxes first
        setCurrentGame("normal");//setting to normal returns normal answers
         Levels loadGame = new Levels(returnCurrentGame());

        int[] gameNums = loadGame.getLevelNums();
        int inc = 1;
        foreach (int i in gameNums)
        {

            if (i != 0)
            {
                TextBox tb = (TextBox)FindControl("TextBox" + inc);
                tb.Text = i.ToString();
                tb.Enabled = false;
            }
            else
            {
                //leave blank and move to next box
            }

            inc++;
        }

    }

Clicking this button changes the numbers in different boxes.

 protected void Button1_Click(object sender, EventArgs e)
    {

    }

Then I have this empty button but everytime I click it, it resets the form even though I havent set it to do anything yet. I would like the boxes to stay the same and I would also like to keep the objects alive. I'm not sure what I'm missing but please point me in the right direction. Thanks in advance

Dah Problum
  • 141
  • 1
  • 2
  • 6

1 Answers1

2

The Page_Load event occurs every time the page is loaded, including event-driven postbacks (button clicks, etc).

It looks like initialization code is in your Page_Load, so when you click the button it runs again.

There are two options:

  • Put everything that you want to happen only on the FIRST load in a n if statement:
  • Move your initialization to Page_Init.

Code sample for the first option:

 protected void Page_Load(object sender, EventArgs e)
    {
      if(!Page.IsPostBack)  // Teis is the key line for avoiding the problem
      {
        Levels loadGame = new Levels(currentGame);

        int [] gameNums =  loadGame.getLevelNums();
        int inc = 1;
        foreach(int i in gameNums){

            if (i != 0)
            {
                TextBox tb = (TextBox)FindControl("TextBox" + inc);
                tb.Text = i.ToString();
                tb.Enabled = false;
            }
            else {
                //leave blank and move to next box
            }

            inc++;
        }
      }
     }

Also, recommended reading: The ASP.NET Page Lifecycle

David
  • 72,686
  • 18
  • 132
  • 173