Click here to Skip to main content
16,017,069 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionStatic Varible Sharing Problem Pin
Shivan Nandan9-Jun-09 21:03
Shivan Nandan9-Jun-09 21:03 
AnswerRe: Static Varible Sharing Problem Pin
Christian Graus9-Jun-09 21:11
protectorChristian Graus9-Jun-09 21:11 
AnswerRe: Static Varible Sharing Problem Pin
saanj9-Jun-09 21:13
saanj9-Jun-09 21:13 
GeneralRe: Static Varible Sharing Problem Pin
Christian Graus9-Jun-09 21:21
protectorChristian Graus9-Jun-09 21:21 
GeneralRe: Static Varible Sharing Problem Pin
Shivan Nandan9-Jun-09 21:25
Shivan Nandan9-Jun-09 21:25 
GeneralRe: Static Varible Sharing Problem Pin
Colin Angus Mackay9-Jun-09 21:31
Colin Angus Mackay9-Jun-09 21:31 
GeneralRe: Static Varible Sharing Problem Pin
saanj9-Jun-09 21:31
saanj9-Jun-09 21:31 
AnswerRe: Static Varible Sharing Problem Pin
Colin Angus Mackay9-Jun-09 21:24
Colin Angus Mackay9-Jun-09 21:24 
Shivan Nandan wrote:
The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a,
i.e., they loose the value what they had set for a. They get the value that is been set by the last user.


That is the correct functionality. ASP.NET applications are multi-user applications. static variables are global for the entire application. If you set it to some value then all users will see that value.


Shivan Nandan wrote:
If I remove 'Static' from its declaration, then its value is not available to other functions.


Incorrect. Its value is available to the methods in the class. This time however, the value only lasts as long as the object it is in lasts (in this case the object is the one representing your page).


Shivan Nandan wrote:
How to solve this problem.


Looking at your sample code, you expect the value to hang around for postbacks. That isn't going to happen. The page object (along with your variable) will exist for as long as it takes to process and render the page. The page object is them discarded (along with your variable).

So, basically, you need some mechanism to store the value between postbacks. You can store the value in the page itself (a hidden field, or add it to the viewstate). You could store it in the session. You could create your own mechanism. It is up to you.

Ultimately, you need ot read a book on how ASP.NET works, and especially how the page lifecycle works as this leaky abstraction trips up many people. In other words HTTP is stateless, but ASP.NET tries to provide a stateful framework, and sometimes it just doesn't work they way you'd expect. So you have to go and read some stuff about the page lifecycle to understand what is going on.

Man who stand on hill with mouth open wait long time for roast duck to drop in

GeneralRe: Static Varible Sharing Problem [modified] Pin
Shivan Nandan9-Jun-09 21:38
Shivan Nandan9-Jun-09 21:38 
QuestionHow To Log when user clicks on the cross button through Ajax Pin
shimona 29-Jun-09 20:54
shimona 29-Jun-09 20:54 
AnswerRe: How To Log when user clicks on the cross button through Ajax Pin
saanj9-Jun-09 21:05
saanj9-Jun-09 21:05 
AnswerRe: How To Log when user clicks on the cross button through Ajax Pin
Sujay chakraborty9-Jun-09 21:17
Sujay chakraborty9-Jun-09 21:17 
QuestionManipulating XML file using GridView and XmlDataSource Pin
Sujay chakraborty9-Jun-09 20:33
Sujay chakraborty9-Jun-09 20:33 
AnswerRe: Manipulating XML file using GridView and XmlDataSource Pin
Abhijit Jana9-Jun-09 21:11
professionalAbhijit Jana9-Jun-09 21:11 
GeneralRe: Manipulating XML file using GridView and XmlDataSource Pin
Sujay chakraborty9-Jun-09 21:21
Sujay chakraborty9-Jun-09 21:21 
GeneralRe: Manipulating XML file using GridView and XmlDataSource Pin
chaitanya2313-Jan-10 20:15
chaitanya2313-Jan-10 20:15 
Questionconfiguration error... Pin
RajpootRohan9-Jun-09 20:16
professionalRajpootRohan9-Jun-09 20:16 
AnswerRe: configuration error... Pin
Abhijit Jana9-Jun-09 20:21
professionalAbhijit Jana9-Jun-09 20:21 
GeneralRe: configuration error... Pin
RajpootRohan9-Jun-09 20:30
professionalRajpootRohan9-Jun-09 20:30 
AnswerRe: configuration error... Pin
Christian Graus9-Jun-09 20:25
protectorChristian Graus9-Jun-09 20:25 
GeneralRe: configuration error... Pin
RajpootRohan9-Jun-09 20:32
professionalRajpootRohan9-Jun-09 20:32 
AnswerRe: configuration error... Pin
saanj9-Jun-09 20:26
saanj9-Jun-09 20:26 
GeneralRe: configuration error... Pin
RajpootRohan9-Jun-09 20:34
professionalRajpootRohan9-Jun-09 20:34 
GeneralRe: configuration error... Pin
saanj9-Jun-09 20:38
saanj9-Jun-09 20:38 
GeneralRe: configuration error... Pin
RajpootRohan9-Jun-09 20:42
professionalRajpootRohan9-Jun-09 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.