How do I store values as objects in Web Forms?

1

How can I store the values in my class?

Every time I click the buttons my class seems to be instantiated again.

My class:

public class Partida
{
    public string[] linha1 = new string[3];
    public string[] linha2 = new string[3];
    public string[] linha3 = new string[3];
    public string[] diagonal1 = new string[3];
    public string[] diagonal2 = new string[3];
    public string[] coluna1 = new string[3];
    public string[] coluna2 = new string[3];
    public string[] coluna3 = new string[3];

    public Partida()
    {
        for (int i = 0; i < 3; i++)
        {
            linha1[i] = "";
            linha2[i] = "";
            linha3[i] = "";
            coluna1[i] = "";
            coluna2[i] = "";
            coluna3[i] = "";
            diagonal1[i] = "";
            diagonal2[i] = "";
        }
    }
}

My Web Page

namespace prova.jogo_da_velha
{
    public partial class jogodavelha : System.Web.UI.Page
    {

        Partida partida = new Partida();

        //True = X False = O
        bool turno = true;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            if (turno)
            {
                partida.linha1[0] = "x";
                partida.coluna1[0] = "x";
                partida.diagonal1[0] = "x";
                Button11.Text = "X";
                turno = false;
            }
            else
            {
                partida.linha1[0] = "o";
                partida.coluna1[0] = "o";
                partida.diagonal1[0] = "o";
                Button11.Text = "O";
                turno = true;
            }
        }

        protected void Button12_Click(object sender, EventArgs e)
        {
            if (turno)
            {
                partida.linha1[1] = "x";
                partida.coluna2[0] = "x";
                Button12.Text = "X";
                turno = false;
            }
            else
            {
                partida.linha1[1] = "o";
                partida.coluna2[0] = "o";
                Button12.Text = "O";
                turno = true;
            }
        }
}
    
asked by anonymous 30.11.2014 / 23:02

1 answer

2

You need to understand the lifecycle of a page in Web Forms (see this link ).

Whenever the browser asks to load a page, ASP.NET creates an instance of its class, it does the operation corresponding to the cycle of your page (eg, if it is the first time, it will probably be a Page_Load with the IsPostBack = false property) and then throws away this object. If it is another action, in addition to calling Page_Load , it will set the IsPostBack property and then call its specific event (as in Button11_Click ).

In order for information to be save (that is, persist) between calls, you need to store the information you want to remain saved on special objects . In this case your instance of class partida and variable turno .

These objects are:

  • View State - Saves information regarding the page you are on. This information ends up going to the user scrambled in a <input hidden , and ASP.NET automatically places that and retrieves it again in Page_Load . Usually the buttons, texts on the screen already use this information in one way or another. It is good to use for information regarding the things of the same screen: which button is clicked, what text is inserted, etc.

  • Session - Saves instances of objects on the server that should be temporarily stored while the user is logged on. It is safer because this information does not go to HTML, it is only on the server. It is the ideal place for you to store these properties. They are shared between pages .. so if you set a Session['partida'] = new Partida() , other pages can also retrieve them. This object is created 1 per user accessing the site,

There are others, such as a special "session" called Application , which is an object relative to all connected users, not just 1.

    
01.12.2014 / 16:15