Best way to save variables between postbacks asp.net?

后端 未结 2 1695
庸人自扰
庸人自扰 2021-01-04 13:03

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?

相关标签:
2条回答
  • 2021-01-04 13:43

    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.

    0 讨论(0)
  • 2021-01-04 13:53
    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";

    0 讨论(0)
提交回复
热议问题