简体   繁体   中英

how can I access Session in preinit function in asp.net?

 void Page_PreInit(Object sender, EventArgs e)
{
    HttpCookie userInfo;
    userInfo = Request.Cookies["userInfo"];
    Session["EmpID"] = userInfo["EmpID"];
    Session["GroupID"] = userInfo["GroupID"];
    Session["DeptID"] = userInfo["DeptID"];
    Session["SecID"] = userInfo["SecID"];

    if (Session["GroupID"] =="1")
    {
        this.MasterPageFile = "master/hr_dept.Master";
    }
    else if (Session["GroupID"] == "2")
    {
        this.MasterPageFile = "master/hr_dept.Master";
    }
    else if (Session["GroupID"] == "3")
    {
        this.MasterPageFile = "master/hod_dept.Master";
    }
    else if (Session["GroupID"] == "4")
    {
        this.MasterPageFile = "master/default2_dept.Master";
    }
    else
    {
        this.MasterPageFile = "master/site.Master";
    }
}

I want to check my session value and then load the master page according to it, I am using same master page on the different pages.

Allow your class to implement IRequiresSessionState such as:

public partial class YOUR_ASPX: System.Web.UI.Page , IRequiresSessionState
{
 // your preinit code

}

this is a flag interface which means not need to implement anything but allows you to access Session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM