How can I save variables in asp.net between postback?
I’m using HttpContext.Current.Items but it always disposes after postback is there any other option to do that?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If your variable is not serializable or you do not want the client to be able to read its value => use inProc Session
If your variable is serializable and you do not want the client to be able to read its value => use database Session
if your variable is serializable, and you can live with the client reading its value, and it should only live during the postbacks sequence in the scope of the page => you should use ViewState.
Method 2
ViewState["YourVariable"] = "123";
ViewState collection is mean for this purpose, in above example YourVariable is a variable whoes value you want to save and 123 is value of this variable.
ViewState is accessible within the scope of page. If you want to have values between different pages you can use sessions like ViewState[“YourVariable”] = “123”;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0