473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
8 9162
"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
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
"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
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
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul | last post by:
Title: What are the Consequences of Aspx page separate from app DLL Hi JL; I am working on a big asp.net application. When we migrate the dll (or dlls) to the production server, all users who are...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
2
by: simon | last post by:
hello, what i'm looking to do is store the path of the app on a the server for reuse in the site. my thoughts so far are... -make a key in the web.config file -retrieve the value in globals.asax...
6
by: BizWorld | last post by:
Hi, I have a scenario where i need to configure only Login.aspx page to use SSL. All other application will run on HTTP protocol. If someone can guide me how to accomplish this. One of my idea...
17
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the...
5
by: Fernando Chilvarguer | last post by:
I'm sure this has come up before but I could not find any post on it. How can I read a variable or property that has been set on a ASPX page from inside a ASCX control. ASPX code: public...
1
by: yanni | last post by:
Hello, I'm creating asp.net 2.0 web site app, I tried to use ¡°ObjectIssue.Page1¡± as TypeName of ObjectDataSource, received error message ¡°The type specified in the TypeName property of...
2
by: 111111222222 | last post by:
I have a grid view with a SSN column as a hyperlink. Whenever I click SSN I want to populate grid view in same page for that particular SSN. For this I need to pass SSN from aspx page to code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.