Reputation: 699
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?
Upvotes: 13
Views: 29004
Reputation: 148120
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";
Upvotes: 10
Reputation: 15413
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.
Upvotes: 12