Nicholas Mordecai
Nicholas Mordecai

Reputation: 889

ASP.Net C# Variables not storing on web page

So i have the code..

int var1 = 0;

protected void cmdvar1_Click(object sender, EventArgs e)
    {
        var1= var1+ 10;
        lblvar1.Text = var1.ToString();
    }

And when clicking this, its great.. it takes the int, adds 10 to it, then displays it.. however it won't display any more than 10, did some playing around and came to the conclusion, that its not because the label isnt updating, it just simply isnt adding 10 to the previous 10 on the variable. What am i doing wrong? What am I mising? Do i need to store the variable info in a cookie or something?

Upvotes: 2

Views: 2676

Answers (5)

Adam
Adam

Reputation: 3914

Use Session body, HTTP is a stateless Protocol, once you postback you loose the current variable value,

Solution:

int var1=0;
protected void cmdvar1_Click(object sender, EventArgs e)
    {

if(Session["var1"]!=null)
var1=int.Parse(Session["var1"].ToString());
else
var1=0;
        var1= var1+ 10;
        lblvar1.Text = var1.ToString();
Session["var1"]=var1;
    }

Upvotes: 1

James
James

Reputation: 82096

This is due to the lifecycle of ASP.NET. Storing private fields behind the web page isn't the same as how it works with WinForms. If you want to persist information across post backs you need to store it in some persistent storage i.e. Session/ViewState/Database etc.

private int var1 = 0;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // load var1 from cache
        Int32.TryParse((Session["var1"] ?? 0).ToString(), out var1);
    }
}

protected void cmdvar1_Click(object sender, EventArgs e)
{
    var1 += 10;
    Session["var1"] = var1; // cache var1
    lblvar1.Text = var1.ToString();
}

Upvotes: 4

Michael O'Neill
Michael O'Neill

Reputation: 954

As suggested in the comments, you have to wrap your universe around statelessness before you will get to producing meaningful web applications.

The ASP.NET equivalent to accomplish state-like behavior is to use the Session collection which is available to every web page.

protected void cmdvar1_Click(object sender, EventArgs e)
{
   int var1 = (int)Session["yourInteger"];
   var1 += 10;
   Session["yourInteger"] = var1;
   lblvar1.Text = var1.ToString();
}

You are obviously setting an initial value for Session["yourInteger"] somewhere else, just one time.

The problem with Session is that it makes your application potentially buggy and somewhat unscalable. The more you use it, the worse on both accounts.

Upvotes: 1

atbebtg
atbebtg

Reputation: 4083

Assuming that lblvar1 is a Label control then you can do the following. The reason this will work is because .NET automatically take care of the state of UIControl

protected void cmdvar1_Click(object sender, EventArgs e)
    {
        var var1 = Convert.ToInt32(lblvar1.Text);
        var1= var1+ 10;
        lblvar1.Text = var1.ToString();
    }

Upvotes: 1

bluetoft
bluetoft

Reputation: 5443

So I would strongly suggest looking into a different platform. Perhaps ASP.NET MVC... However you can use something like the following to get arround your problem.

private int MyNum
{
   get{ return (int)ViewState["MyNum"] ?? 0; }
   set { ViewState["MyNum"] = value; }
}

Then just use MyNum as your integer your incrementing.

Upvotes: 2

Related Questions