Re: variable scope in a webform only
- From: "Teemu Keiski" <joteke@xxxxxxxxxxxxxxx>
- Date: Sat, 10 Mar 2007 14:09:37 +0200
That's because Page class is instantiated for every request so classes declared on page class body, are in the same instance only during a single request. On next request (subsequent postback is such also) it is a new instance and therefore new set of variables. If you want something that survives for subsequent postbacks use ViewState.
As was said, do *not* use static(Shared) variables on Page class. They bring more harm than good.
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"Max" <massimiliano.aronica@xxxxxxxxxxxxx> wrote in message news:UWwIh.9457$o%.8618@xxxxxxxxxxxxxxxxxxxxxxxx
Hi Scott
I tried that, but it seems that the variable cannot be seen by all the SUBs of that page.
If for example, Button1_click sets abc="hello" , Button2_click cannot see abc value.
How do you declare abc inside the page class?
Thanks!
Max
"Scott M." <s-mar@xxxxxxxxxxxxx> ha scritto nel messaggio news:ed8hMcpYHHA.4396@xxxxxxxxxxxxxxxxxxxxxxxJust declare the variable inside the page class, but not inside of any particular method and you'll be able to call the variable from anywhere inside the page class. If another browser window were to be opened, it would be a different instance of the class and so one variable's value wouldn't affect the other.
"Max" <massimiliano.aronica@xxxxxxxxxxxxx> wrote in message news:cnkIh.8162$o%.1828@xxxxxxxxxxxxxxxxxxxxxxxxPlease somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only webform?
I mean , I d like to see that variable from all the Sub of that webform code, but if I window.open another instance of that webform, that variable should be independant even if the name is the same.
I've declared that var as Public shared, but I realize now that that variable is modifiable from all the webforms, and even from another user session! Is this possible?
Should I use viewstate, or there is a right declaration that I should use and that I don't know?
Thanks Max
.
- Follow-Ups:
- Re: variable scope in a webform only
- From: Scott M.
- Re: variable scope in a webform only
- From: Mark Rae
- Re: variable scope in a webform only
- References:
- variable scope in a webform only
- From: Max
- Re: variable scope in a webform only
- From: Scott M.
- Re: variable scope in a webform only
- From: Max
- variable scope in a webform only
- Prev by Date: Re: variable scope in a webform only
- Next by Date: Re: Can I reconnect to a session that has timed out or been abandoned
- Previous by thread: Re: variable scope in a webform only
- Next by thread: Re: variable scope in a webform only
- Index(es):