ASP.Net Static Value keep accumulate while refreshing the page

1.1k views Asked by At

I have an asp.net page, and a static value totalBalance that sums the values in a column in a gridview.

I found, when I refresh the page, the totalBalance get accumulated instead of keep the original value.

Is there any code I could insert so that it can refresh the values, and each time I refresh the page, it is re-calculating the column values instead of accumulating numbers?

I currently have this RemoveCache

protected void RemoveCache()
    {
        Response.CacheControl = "no-cache";
        Response.AddHeader("Pragma", "no-cache");
        Response.Expires = -1;
    }

Can I insert some code in this or the aspx to reset the value after running please?

Thanks.

Never mind, I set totalBalance=0 when loading the page....

1

There are 1 answers

0
Mike Cheel On BEST ANSWER

A static variable is a variable that has one copy of it (which means shared throughout the application) and its lifetime is the same as the application, once instantiated. Regardless of refresh, the variable is the same one from the first time it was created and you are re-using and re-totaling a running value. I would say stop using static variables in your web applications unless you really understand the implications and the problem should go away.