How do I store values as objects in Web Forms?

Asked

Viewed 360 times

1

How can I store the values of 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;
            }
        }
}
  • 1

    You must [Dit] the question and put your code (put relevant parts, no more, no less). It is difficult for people to work with a screenshot. And please learn to make titles described of your problem, it is not to play the tags in it. Also choose the tags with more care. You will find it difficult to be helped if you don’t collaborate. And try to clarify the question, I don’t understand what you want effectively. It seemed to me that you want to know something unrelated to your problem.

  • That’s better?

  • I got a formatting problem. TAKE a look at the edition. http://answall.com/posts/42407/revisions

1 answer

2

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

Every time the browser asks you to load a page, ASP.NET creates an instance of its class, does the operation corresponding to the cycle of your page (example, if it is the first time, it will probably be a Page_Load with the property IsPostBack = false) and then throw away this object. If it is another action, besides calling the Page_Load, he arrow the property IsPostBack and then calls your specific event (as in the case, Button11_Click).

So that information can be salvage (i.e., persist) between a call and another, you need to store the information you want to stay safe in special objects. In this case your class instance partida and the variable turno.

These objects are:

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

  • Session - Saves on the server instances of objects that must be stored temporarily while the user is logged in. 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 one Session['partida'] = new Partida(), other pages may also retrieve them. This object is created 1 per user who accesses the site,

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.