By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,666 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,666 IT Pros & Developers. It's quick & easy.

How to preserve the value of a variable on the same aspx page?

P: n/a
hb
Hi,

I need to declare a variable who's value can be preserve through the same
ASP.Net page.
I tried the following code, only the static variable s2 keeps its value=22
after lnk1_Click
followed by lnk2_Click(). The others return to their original values
initiated during their
declaration. But the static variable's life doesn't end when the page is
closed and it causes
problem.
===
//======upsCont.aspx======
<%@ Page language="c#" Codebehind="upsCont.aspx.cs" AutoEventWireup="false"
Inherits="MyWeb.test.upsCont" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head></head><body>
<form id="Form1" method="post" runat="server">
<asp:linkbutton runat="server" id="lnk1">link one</asp:linkbutton>
<br />
<asp:linkbutton runat="server" id="lnk2">link two</asp:linkbutton>
</form>
</body></html>

//========upsCont.aspx.cs=======
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWeb.test
{
public class upsCont : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton lnk1;
protected System.Web.UI.WebControls.LinkButton lnk2;
int s1=0;
static int s2=0;
protected int s3=0;
internal int s4=0;
public int s5=0;
protected internal int s6=0;

private void Page_Load(object sender, System.EventArgs e)
{

Response.Write("start:s1="+s1+"||s2="+s2+"||s3="+s 3+"||s4="+s4+"||s5="+s5+"|
|s6="+s6+"<br>");
}

private void lnk1_Click(object sender, System.EventArgs e)
{
s1=11;
s2=22;
s3=33;
s4=44;
s5=55;
s6=66;
Response.Write("link1:s1="+s1+"||s2="+s2+"||s3="+s 3+"||s4="+s4+"||s5="+s5+"|
|s6="+s6+"<br>");
}

private void lnk2_Click(object sender, System.EventArgs e)
{

Response.Write("link2:s1="+s1+"||s2="+s2+"||s3="+s 3+"||s4="+s4+"||s5="+s5+"|
|s6="+s6+"<br>");
}
}
}
===
Are there any other types of variable that keep the variable's value updated
through the
same page and end the life of the variable when page is closed? ( I don't
want to use
neither cookies nor sessions)

Thank you

hb

Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"hb" <ho****@goodoffices.com> wrote in news:uqq3Pb8qEHA.2856
@TK2MSFTNGP10.phx.gbl:
I need to declare a variable who's value can be preserve through the same
ASP.Net page.


You can use:

Cookies
Session Variables
Viewstate

http://msdn.microsoft.com/library/de...l=/library/en-
us/vbcon/html/vbconChoosingServerStateOption.asp

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #2

P: n/a
hb
I know these three can do the job. But their can be used through the whole
application. Sometimes, this causes confusion.

I am looking for something like a page variable or local variable to do the
job. Are there any other options?

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"hb" <ho****@goodoffices.com> wrote in news:uqq3Pb8qEHA.2856
@TK2MSFTNGP10.phx.gbl:
I need to declare a variable who's value can be preserve through the same ASP.Net page.


You can use:

Cookies
Session Variables
Viewstate

http://msdn.microsoft.com/library/de...l=/library/en-
us/vbcon/html/vbconChoosingServerStateOption.asp

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #3

P: n/a
"hb" <ho****@goodoffices.com> wrote in
news:#K**************@TK2MSFTNGP11.phx.gbl:
I know these three can do the job. But their can be used through the
whole application. Sometimes, this causes confusion.
This is where you're confused... ASP.NET for the most part is stateless.
Once a page is generate all variables are destoryed. As a result there
are no "native" variables which can persist over multiple page views or
sessions. To maintain session values in ASP.NET you must use one of the
following:

Application - Persistent until server shutdown for all sessions
Cookies - Persistent over multiple sessions for a user
Session - Persistent only for current session for a user
ViewState - Persistent for the current page for a user
Database, etc.

I am looking for something like a page variable or local variable to
do the job. Are there any other options?


So what you probably want is ViewState variable. A viewstate variable is
only valid for the current page, current view, current user.

Once a user navigates away from the page, the viewstate is lost.

The viewstate is stored as a hidden form field on the current webpage
and is automatically parsed by ASP.NET.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #4

P: n/a
hb
Lucas ,

Thank you for the clarification.

Do you mean that only static variable can preserve an update
its value through different events on the same page and
all other type of variables like private, internal, protected, etc.
will lost their previous value through different events on the same page?

hb
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"hb" <ho****@goodoffices.com> wrote in
news:#K**************@TK2MSFTNGP11.phx.gbl:
I know these three can do the job. But their can be used through the
whole application. Sometimes, this causes confusion.


This is where you're confused... ASP.NET for the most part is stateless.
Once a page is generate all variables are destoryed. As a result there
are no "native" variables which can persist over multiple page views or
sessions. To maintain session values in ASP.NET you must use one of the
following:

Application - Persistent until server shutdown for all sessions
Cookies - Persistent over multiple sessions for a user
Session - Persistent only for current session for a user
ViewState - Persistent for the current page for a user
Database, etc.

I am looking for something like a page variable or local variable to
do the job. Are there any other options?


So what you probably want is ViewState variable. A viewstate variable is
only valid for the current page, current view, current user.

Once a user navigates away from the page, the viewstate is lost.

The viewstate is stored as a hidden form field on the current webpage
and is automatically parsed by ASP.NET.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #5

P: n/a
"hb" <ho****@goodoffices.com> wrote in news:Oqx7aQ#qEHA.2340
@TK2MSFTNGP11.phx.gbl:
Do you mean that only static variable can preserve an update
its value through different events on the same page and
all other type of variables like private, internal, protected, etc.
will lost their previous value through different events on the same page?


I'm confused... do you want a variable that works across postbacks, or do
you want a class level variable (a variable that is valid for the entire
class)?

ASP.NET has class level variables (as you have declared them in your post:

public class upsCont : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton lnk1;
protected System.Web.UI.WebControls.LinkButton lnk2;

As for Static variables, they shouldn't persist after a Postback... for
Postbacks, you must use ViewState, Session variables, etc. Some webcontrols
like Textboxes, Datagrids, etc have Viewstate implemented behind the
scenes.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #6

P: n/a
hb
Hi, Lucas,

Sorry about the confusion. I always get confused by the mechanism of
PostBack.
What I need is this: a variable (ex, var) declared at page class level with
initial value (ex, var=0),
then it get new value in lnk1_Click() event (ex, var=1), then when
lnk2_Click() event
happens, the variable should have the value 1 instead of 0.

During my test in the code posted in my original message, the static
variable (s2) is the only one
that can do the job. I thought when the page is closed, the life of page
class initiated object ends
so as all variables declared at page class level. But the life time of
static variable declared at page
class level seems go beyond the life time of the page itself. The fact is: I
close the page and open it
again within a short period time, the value of static variable is still
there. This causes a lot of problem.
That's why I have to find something else to replace all static variables
declared at page class level.

But I don't like to use Session or ViewState variables for memory concern. I
don't like to use cookies
for security concern.

As you mentioned, if the ViewState is the only option at this situation, I
will not have choice but use it.

Thank you

hb
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"hb" <ho****@goodoffices.com> wrote in news:Oqx7aQ#qEHA.2340
@TK2MSFTNGP11.phx.gbl:
Do you mean that only static variable can preserve an update
its value through different events on the same page and
all other type of variables like private, internal, protected, etc.
will lost their previous value through different events on the same
page?
I'm confused... do you want a variable that works across postbacks, or do
you want a class level variable (a variable that is valid for the entire
class)?

ASP.NET has class level variables (as you have declared them in your post:

public class upsCont : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton lnk1;
protected System.Web.UI.WebControls.LinkButton lnk2;

As for Static variables, they shouldn't persist after a Postback... for
Postbacks, you must use ViewState, Session variables, etc. Some webcontrols like Textboxes, Datagrids, etc have Viewstate implemented behind the
scenes.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #7

P: n/a
"hb" <ho****@goodoffices.com> wrote in news:OD3K7E$qEHA.3172
@TK2MSFTNGP10.phx.gbl:
I thought when the page is closed, the life of page
class initiated object ends
so as all variables declared at page class level.


I just did a google search and static variables are an exception. Static
variables are .NET's new application variables - they persist for the life
time of an application (and I think they're shared across all sessions
too).

http://www.devx.com/vb2themax/Tip/18845

You learn something new every day : )
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #8

P: n/a
hb
Thank you!
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"hb" <ho****@goodoffices.com> wrote in news:OD3K7E$qEHA.3172
@TK2MSFTNGP10.phx.gbl:
I thought when the page is closed, the life of page
class initiated object ends
so as all variables declared at page class level.


I just did a google search and static variables are an exception. Static
variables are .NET's new application variables - they persist for the life
time of an application (and I think they're shared across all sessions
too).

http://www.devx.com/vb2themax/Tip/18845

You learn something new every day : )
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.