简体   繁体   中英

session does not work to control panel

I have problem with session, I have 2 login pages, one for Admin And one for teacher, when user enter his Id and password, the session created for his name, I used this session in Masterpage so I can control with the appearance of some panels showing url to another pages, but unfortunately it doesn't work, I thought to create different master page for Admin and teacher, but this is a bad solution where some pages common between two, anyway this is the code firstly in master page

protected void Page_Load(object sender, EventArgs e)
{ 

if (Session["fname"] == "admin")
{
    Panel2.Visible = false;
    Panel1.Visible = true;
    Panel3.Visible = false;
}
else if (Session["fname"] == "Teacher")
{
    Panel1.Visible = false;
    Panel2.Visible = true;
    Panel3.Visible = false;

}
else
{
    Panel1.Visible = false;
    Panel2.Visible = false;
    Panel3.Visible = true;
}
}

and in admin login page

string s = "Select ID,Name,Password FROM Admin where ID = '" + TextBox1.Text + "'";
    con.Open();
    SqlCommand cmd = new SqlCommand(s, con);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        dr.Read();
        string s1 = dr[2].ToString();
        string s2 = TextBox2.Text;
        if (s1 == s2)
        {
            Session["fname"] = "admin";


            Response.Redirect("ManageCourse.aspx", true);
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Login Successful');", true);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

And in teacher login page

 string s = "Select ID,Name,Password FROM Teacher where ID = '"+TextBox1.Text+"'";
        con.Open();
        SqlCommand cmd = new SqlCommand(s,con);
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            dr.Read();
            string s1=dr[2].ToString();
            string s2=TextBox2.Text;
            if (s1 == s2)
            {
                Session["fname"] = "Teacher";


                Response.Redirect("AddQ.aspx", false);
                Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Login Successful');", true);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }

and I added in webconfig this line

 <sessionState mode="InProc"/>

The desired result that only panel their visible set to true appear, but sessions doesn't work and it only go to the last else where panel3 only appear.

Convert the content of session to string. use method ToString()

if (Session["fname"].ToString() == "admin")

else if (Session["fname"].ToString() == "Teacher")

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