I have one asp.net page with next and back button and a variable "i", each time I click next or back button, variable "i" will increase or decrease by 1. So how do I declare variable i? I don't want to use "public static int i" Should I use session or viewstate? Are there any better ways to do this?
5 Answers
The View State is the state of the page and all its controls. It is automatically maintained across posts by the ASP.Net framework.
Every time the page posts back, it is essentially starting over from scratch - anything initialized to 0, for example, will be zero again. This is because the server doesn't know anything about the last time the page ran - all it knows is you clicked a button which submits a form to that.
When a page is sent back to the client, the changes in the properties of the page and its controls are determined and stored in the value of a hidden input field named _VIEWSTATE. When the page is again post back the _VIEWSTATE field is sent to the server with the HTTP request.
AS on Incrementing variables in ASP.net on button click
If you need to persist a value across postbacks, the standard method is to use
ViewState:Public Property MyCounter() As Integer Get Dim val As Object = ViewState("MyCounter") Return If(val IsNot Nothing, CInt(val), 0) End Get Set(ByVal value As Integer) ViewState("MyCounter") = value End Set End PropertyIt's also possible to use Session, which will persist the value across all pages and requests for the life of the user's session. For that to work, you can use the same sample above, replacing ViewState with Session.
Comments
You can use ViewState as follows if you want to use it on the same page
private int i
{
get
{
return ViewState["i"] != null ? (int)ViewState["i"] : 0;
}
set
{
ViewState["i"] = value;
}
}
and use it
protected void next_Click(object sender, EventArgs e)
{
i++;
}
protected void back_Click(object sender, EventArgs e)
{
i--;
}
Comments
A Session would be a good idea to store the value.
A session is stored on the server and thus cannot be tampered with.
Note that an ID of the session is stored in a cookie on your computer.
But, even when you disable cookies, your Session would still work as your sessionID will be passed as a QueryStringin your URL then.
2 Comments
Is it ajax buttons?
If not and you trying to implement some kind of pagination it is not the best solution. Why not just send new i value in query string?
-include "i" value in url path:
/Page/<i>
instead of
/Next
etc.
If it ajax - you still able to send "i" value in request body or in querystring.
Comments
Assuming you need the value of "i" on more than one page:
I would suggest you to use a Session variable.
http://msdn.microsoft.com/en-us/library/vstudio/ms178581(v=vs.100).aspx
Sessionvariable unless you need to persist the values across pages. If your concern is limited to a particular page then use aViewStatevariable (wrapped in a property for ease of use), not a Session variable. There are multiple problems involved with use of session variables and are best to avoid wherever possible.ViewStateandHidden Fieldare immune to, since they do a roundtrip. Also, session variables are hard to cleanup once they are no longer required. e.g. you navigate to another page, but your session variable will still be alive.