简体   繁体   中英

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?

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 ie 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();
}

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.

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;
    }

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();
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM