I am working on a website with .aspx pages and code behind in VB.Net. On a page I need a Public Shared variable like
Public Shared comments As String() = New String(2) {"", "", ""}
since I need the persistence of the value of “comments” through post backs of the page. At least I think having it Public Shared is the only way to keep the persistence, unless you guys tell me that I am wrong.
But on the other hand I don’t want other users who might be using the same page conflict their “comments” data with other users. How can I achieve this?
It depends on the level of persistence you need.
If you only need the comments to live on the page until the user has navigated away from it, use your page’s ViewState:
If you’d like the values to persist for the page between navigating away and newly landing on the page again (i.e. initial load rather than postback), use your Session state bag:
If you’d like this to persist even after the session is expired and the user returns later, you’d need to write to a backing store that would link the user to their comments such as a database, or–and I say this with much less enthusiasm–write out to something like an XML file, and then load the value on your initial page load in a statement such as:
When using a backing store, however, I generally recommend using one of the data source controls along with a databound control such as a ListView or GridView to display the information.