I am developing an asp.net web site. I have an aspx page. I want to use a variable whose value shouldn’t be resetted during a postback, but when pass to another page and turn back it should be resetted. And also I’am changing this variables value in the code behind in C#. So it should change when I want, it shouldnt be resetted during a postback and it should be resetted when I navigate to another page.
I tried to use
public int
public static int
protected int
but I couldnt realize which one is working right.
The ability to preserve data over post backs isn’t really up to the variable scope.
It really doesn’t matter if you choose public, protected or private.
The technique you are looking for is either called Session State or View State.
Session statepreserves data across different pages andview statewithin the same page. There is actually also a 3rd, the Application state object which preserves data globally across different user sessions as well.In the most simple form, you would user a view state like:
and after the post back , in the
Page_Loadmethod: