473,498 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable persistence on postback

(.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!
Nov 19 '05 #1
6 3032
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
"BradC" <br********@yahoo.com> wrote in message
news:51**************************@news.microsoft.c om...
(.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!

Nov 19 '05 #2
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/is...PNETUserState/

Patrice

--

"BradC" <br********@yahoo.com> a écrit dans le message de
news:51**************************@news.microsoft.c om...
(.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!

Nov 19 '05 #3
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

"BradC" wrote:
(.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!

Nov 19 '05 #4
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/is...2/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)

"BradC" wrote:
(.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!

Nov 19 '05 #5
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
"BradC" <br********@yahoo.com> wrote in message
news:51**************************@news.microsoft.c om...
(.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!

Nov 19 '05 #6
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" <br********@yahoo.com> wrote in message
news:51**************************@news.microsoft.c om...
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
"BradC" <br********@yahoo.com> wrote in message
news:51**************************@news.microsoft.c om...
(.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!


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4158
by: Paolo Losi | last post by:
Hi all, I'm pretty new to the python language so please excuse me if this is FAQ... I'm very glad to be part of the list! :-) I'm looking into a way to implement a generic workflow framework...
1
2520
by: Joseph Luner | last post by:
I am having problem postback, (code shown below) the variable "my_str" is lost after clicking the "submit" button. Isn't it suppose to display "changed" after posting back? Is there anyway I...
1
3416
by: Michelle Stone | last post by:
Hi all. I have an empty datagrid on my web form. And I add BoundDataColumn(s) to it through code. But after each postback, the columns and rows disappear. As a workabout, I tried to rebind the...
3
7306
by: Michelle Stone | last post by:
hi al i have "variab1 = window.open ("a.aspx", "helloworld"); i do this inside a RegisterStartupScrict ("key", "<script language=javascript>******</script>"); where ***** is hte javascript...
10
387
by: Simon Harvey | last post by:
Hi everyone, Can anyone tell me if I declare a global variable in my pages code behind, is it persisted if the page does a post back, or do I need to add the object to the session object in...
2
2340
by: dkode | last post by:
Hello, I am laying out the architecture for a very large website that will scale to a very large degree. I have a couple of questions before I attempt to go and implement a Broker/Persistence...
5
7116
by: rockdale | last post by:
Hi, all: I have a linkbutton and I use javascript to open another webpage in a new window. I also want to set my session variable value when this linkbutton get clicked. These session variable...
7
2434
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone? Which is considered to be the 'best' way to cache a dataset and/or a crystal reports reportdocument object? ViewState? Session? Something else? Thanks in advance!
0
2364
myusernotyours
by: myusernotyours | last post by:
Hi all, Am trying to create a Java Desktop App that uses Java Persistence in Netbeans. The database is MS Access but I tried with Mysql and got the same error. When I run the app( Create the...
0
7165
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
7205
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...
1
6887
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
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.