Variable persistence on postback

  • Thread starter Thread starter BradC
  • Start date Start date
B

BradC

(.Net 1.1, Visual Studio 2003, if that matters)

I thought that publically declared variables would persiste their values
on postback of the page. This is server-side code, afterall.

I could stick a simple string in a cookie or stick it on the end of the URL,
but that seems so... "1998". What's the right way to do this in ASP.Net?

What if it is something more complex? A hashtable of page controls indexed
by ID, for example?

Thanks!
 
Brad,

An ASP.NET page class file is destroyed after it is served to the client.
The most chosen method for preserving variables in ASP.NET from one page
load to the next is to store them in viewstate which is a hidden input.

ViewState.Add("MyVariableKey", [MyVariable Value as Object])

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
I could stick a simple string in a cookie or stick it on the end of
the URL, but that seems so... "1998". What's the right way to do this
in ASP.Net?

What if it is something more complex? A hashtable of page controls
indexed by ID, for example?

You could use viewstate or session objects to persist items.

Or take a look at Microsoft's User Interface Process Application Block - it
can do automatic persistence of control values (and even restore them after
the fact)
 
It's server code but it's still based on a server/response model. Each time
a browser hit a page, the page is *created* and runs to produce the HTML
output. This is true even if the page was previously used by the same user
(else the server would have to maintain the state of each page used for all
users for a default 20 mn period).

Now you can simulate this using a DB, cookies, session variable, viewstate
and likely still others.. Try :
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/

Patrice
 
In ASP.NET any variable is destroyed after the page has done running.
You can store values you want to get back in 3 ways.
Either store it on a hidden field in the form
and get the value using the request form or if the hidden field is a server
control you can access it directly

Or you can put it in the page's viewState like so
ViewState("MyValue") = "SomeValue"

Or you can put it in the Session("MyVal") = "SomeValue"
But note that for Session to work the uclient needs to have cookies enabled.
But i beleive you can have cookieless sessions aswell, not sure though
 
public variables are not persisted across post backs..

one new method asp.net support to persisit a variable across postbacks is
"ViewState ". You can add Objects to view state .. but some thing to remeber
is the data added to view state is passed on each request and response
between client and server

this may help http://msdn.microsoft.com/msdnmag/issues/03/02/CuttingEdge/

for non complex data types, The conventional method of using a HTML Hidden
Control or using a TextBox with hidden style works as well.. (this way the
value stored is available in client as well across postbacks)
 
Thanks, Justin (and everyone else!)

Looks like ViewState is the simplest solution for my purposes, since I don't
have to persist the values across other pages. After all, this is what the
ASP.Net server-side controls use to remember their own values, right?

Brad
Brad,

An ASP.NET page class file is destroyed after it is served to the
client. The most chosen method for preserving variables in ASP.NET
from one page load to the next is to store them in viewstate which is
a hidden input.

ViewState.Add("MyVariableKey", [MyVariable Value as Object])

S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
(.Net 1.1, Visual Studio 2003, if that matters)

I thought that publically declared variables would persiste their
values on postback of the page. This is server-side code, afterall.

I could stick a simple string in a cookie or stick it on the end of
the URL, but that seems so... "1998". What's the right way to do this
in ASP.Net?

What if it is something more complex? A hashtable of page controls
indexed by ID, for example?

Thanks!
 
note: viewstate is a hidden field in the page and ties to values stored in
on the render. so using it you bloat pagesize. also if the user navigates
back, the server will get an older viewstate. also it only preserved on a
postback, it lost on any links in your site. this mean you shoudl only store
info in the view that required for postback processing of the same request.


-- bruce (sqlwork.com)


BradC said:
Thanks, Justin (and everyone else!)

Looks like ViewState is the simplest solution for my purposes, since I
don't have to persist the values across other pages. After all, this is
what the ASP.Net server-side controls use to remember their own values,
right?

Brad
Brad,

An ASP.NET page class file is destroyed after it is served to the
client. The most chosen method for preserving variables in ASP.NET
from one page load to the next is to store them in viewstate which is
a hidden input.

ViewState.Add("MyVariableKey", [MyVariable Value as Object])

S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
(.Net 1.1, Visual Studio 2003, if that matters)

I thought that publically declared variables would persiste their
values on postback of the page. This is server-side code, afterall.

I could stick a simple string in a cookie or stick it on the end of
the URL, but that seems so... "1998". What's the right way to do this
in ASP.Net?

What if it is something more complex? A hashtable of page controls
indexed by ID, for example?

Thanks!
 

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

Back
Top