Đức Trần
Đức Trần

Reputation: 89

How to declare common variable for all action in a asp.net page

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?

Upvotes: 1

Views: 1360

Answers (5)

शेखर
शेखर

Reputation: 17614

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

Upvotes: 1

Dave
Dave

Reputation: 434

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

Upvotes: -1

Michael Pryn
Michael Pryn

Reputation: 1

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.

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15923

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.

Upvotes: 1

Complexity
Complexity

Reputation: 5830

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.

Upvotes: 0

Related Questions