1

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?

Đức Trần
  • 89
  • 4
  • 13
  • 1
    Yes, you can use session or viewstate. Never a static field since that value would be the same for every request (user). – Tim Schmelter Oct 07 '14 at 08:10
  • DO NOT use a `Session` variable unless you need to persist the values across pages. If your concern is limited to a particular page then use a `ViewState` variable (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. – Pradeep Kumar Oct 07 '14 at 08:21
  • thank you @TimSchmelter, I know Static variables have Application scope. Can you tell me about problems you mention @PradeepKumar? – Đức Trần Oct 07 '14 at 08:33
  • Session variables with common names like a,b,c or i,j,k etc. can be innocently modified by other pages and you will have a hard time debugging your code. Moreover session variables suffer with timeout problems, which `ViewState` and `Hidden Field` are 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. – Pradeep Kumar Oct 07 '14 at 08:34

5 Answers5

1

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--;
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
1

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 Property

It'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.

Community
  • 1
  • 1
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

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.

Complexity
  • 5,682
  • 6
  • 41
  • 84
0

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.

-1

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

Dave
  • 434
  • 5
  • 22