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

H

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="+s3+"||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="+s3+"||s4="+s4+"||s5="+s5+"|
|s6="+s6+"<br>");
}

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

Response.Write("link2:s1="+s1+"||s2="+s2+"||s3="+s3+"||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
 
H

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?
 
L

Lucas Tam

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.
 
H

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
 
L

Lucas Tam

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.
 
H

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
 
L

Lucas Tam

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 : )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top