Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

like image 573
Abdul Muqeet Avatar asked Dec 06 '25 07:12

Abdul Muqeet


1 Answers

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.

like image 58
Alan Tsai Avatar answered Dec 07 '25 20:12

Alan Tsai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!