c# how to set up and use session state from preinit

后端 未结 3 1491
温柔的废话
温柔的废话 2020-12-20 09:56

OK so to set and read variables from the current session

String Myvar =(string) System.Web.HttpContext.Current.Session[“MyVariable”]

To set

3条回答
  •  渐次进展
    2020-12-20 10:19

    As the comment mentioned, is there some reason you need this in the PreInit event?

    PreInit happens very early in the page lifecycle. It happens, in fact, before even the master page (if any) is applied, before all the controls are fully initialized, etc.

    A much better choice for the majority of applications is in the Load event. If you're still getting a NullReferenceException there, then there's a bigger issue.

提交回复
热议问题