Jackal
Jackal

Reputation: 3521

Global static variable not changing value

I'm trying to make an application where the administrator can change some global values that it will affect the application for every user.

I tried to use static

Class

public static class Inventario
{
    public static int FiltroEtiquetas;

    public static int NumLocalizacoes;

    public static void GetInventario()
    {

    }
}

Page

protected void Page_Load(object sender, EventArgs e)
{

   txtFiltroEtiquetas.Text = Inventario.FiltroEtiquetas.ToString();
   txtNumLocalizacoes.Text = Inventario.NumLocalizacoes.ToString();
}
protected void btnSaveInventaryChanges_Click(object sender, EventArgs e)
{

   Inventario.FiltroEtiquetas = Convert.ToInt32(txtFiltroEtiquetas.Text);
   Inventario.NumLocalizacoes = Convert.ToInt32(txtNumLocalizacoes.Text);
   Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "ShowToast('OK', 'topCenter','success', 'Alterações Guardadas com sucesso.', '2000')", true);
 }

Whenever i open the modal the value is always 0, it won't save the changes

Upvotes: 1

Views: 1053

Answers (2)

Liju L
Liju L

Reputation: 124

I think its really not a good idea to use the static variable in this scenario. I think you are better off using a application variable.

Upvotes: 1

Gaurav
Gaurav

Reputation: 902

On page load you are resetting the both the textboxes with your global variables and on button click you are doing reverse so its kind of reset. That's why you are seeing 0 value.

You need to remove the code from page_load or you need to apply some condition over that.

You can implement something like this:

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
      txtFiltroEtiquetas.Text = Inventario.FiltroEtiquetas.ToString();
      txtNumLocalizacoes.Text = Inventario.NumLocalizacoes.ToString();
   }
}

Upvotes: 2

Related Questions