Confused by the behaviour of static variable
-
Hello Guys I've a datatable whose values need to be maintained across postbacks. (Just single page, not across multiple pages). This datatable is not read only. I mean to say some data can be changed based on user selection during partial postbacks and need to be maintained till the final submit. I declared the datatable as static and could retain the values across partial postbacks. But the problem is that I've always thought that in multiuser environment only one copy of the static variable is created and shared across the users. But my datatable behaves differently. I opened the page in a browser and pressed Ctrl+N for new window. When I work on both windows, the datatable is not clashed. I mean - changes done in one window is not reflected in the other window. This is the behaviour I want exactly. One user's change should not affect other user. This is a reporting page and I store the user selections in the datatable before the final submit. Based on the options in the datatable, the report is created. Though I achieve the desired functionality, I am worried whether what I am doing is right. Is it true that a static variable is shared acorss users or not??? Any inputs? I am using VWD 2008 Express edition and c#. Kit
-
Hello Guys I've a datatable whose values need to be maintained across postbacks. (Just single page, not across multiple pages). This datatable is not read only. I mean to say some data can be changed based on user selection during partial postbacks and need to be maintained till the final submit. I declared the datatable as static and could retain the values across partial postbacks. But the problem is that I've always thought that in multiuser environment only one copy of the static variable is created and shared across the users. But my datatable behaves differently. I opened the page in a browser and pressed Ctrl+N for new window. When I work on both windows, the datatable is not clashed. I mean - changes done in one window is not reflected in the other window. This is the behaviour I want exactly. One user's change should not affect other user. This is a reporting page and I store the user selections in the datatable before the final submit. Based on the options in the datatable, the report is created. Though I achieve the desired functionality, I am worried whether what I am doing is right. Is it true that a static variable is shared acorss users or not??? Any inputs? I am using VWD 2008 Express edition and c#. Kit
By my understanding, it depends on the container that you use. If the static variable is contained in a instantiable class then the variable is not shared across users. If it is contained in a static class then it is shared across users. This is just my understanding and there is every possibility that I might be wrong. A safer and a much cleaner approach to this problem would be to put the datatable in the page's viewstate. This way the datatable along with the changes is available as long as the page is alive and you get peace of mind! HTH!