日期:2014-05-18  浏览次数:20444 次

为何刷新后页面无法判断session对象?如何实现发表留言后自动刷新还能判断session?
我的页面留言后想自动刷新,然后可以查看到新的留言,但是一旦离开这个页面,竟然就无法判断session了。
奇怪的是,可以继续留言,但page_load()里就是无法判断session。

这是我的page_load()

C# code
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack || Convert.ToBoolean(Session["judge"]) == false)
        {
            bind();
            lblName2.Text = "您尚未登录";
        }
        else
        {
            Session["judge"] = true;
            Session["mail"] = txtUserMail.Text;
            Panel1.Visible = false;
            Panel2.Visible = true;
            lblName.Text = "Hello~" + Convert.ToString(Session["mail"]);
            lblName2.Text = Convert.ToString(Session["mail"]) + "您可以发表留言";
 
        }

    }


这是登录按钮
C# code
    protected void Button1_Click(object sender, EventArgs e)
    {
        string dataPath = Server.MapPath("access/DataBase.mdb");
        OleDbConnection con = DB.con(dataPath);
        con.Open();

        OleDbCommand cmd = new OleDbCommand();

        cmd.Parameters.Add("@userMail", OleDbType.VarChar, 50);
        cmd.Parameters["@userMail"].Value = txtUserMail.Text;
        cmd.Parameters.Add("@userPWD", OleDbType.VarChar, 20);
        cmd.Parameters["@userPWD"].Value = FormsAuthentication.HashPasswordForStoringInConfigFile(txtuserPWD.Text, "md5");

        cmd.CommandText = "select count(*) from userinfo where userMail = @userMail and userPWD = @userPWD";
        cmd.Connection = con;

        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (count == 1)
        {
            Session["judge"] = true;
            Session["mail"] = txtUserMail.Text;
            Panel1.Visible = false;
            Panel2.Visible = true;
            lblName.Text = "Hello~"+Convert.ToString(Session["mail"]);
            lblName2.Text = Convert.ToString(Session["mail"])+"您可以发表留言";

            OleDbCommand IDcmd = new OleDbCommand("select ID from userinfo where userMail ='"+txtUserMail.Text+"'", con);
            Session["ID"]=Convert.ToString(IDcmd.ExecuteScalar());
        }
        else
        {
            Session["judge"] = false;
            Response.Write("登录失败!帐号或者密码错误!");
        }
        con.Close();

    }


这是aspx页面panel的html代码
HTML code

<asp:Panel ID="Panel1" runat="server" Height="12px" Width="90%">
                用户名:<asp:TextBox ID="txtUserMail" runat="server"></asp:TextBox>
                密码:<asp:TextBox ID="txtuserPWD" runat="server" TextMode="Password"></asp:TextBox>&nbsp;<asp:Button ID="Button1"
                    runat="server" Text="登录" OnClick="Button1_Click" />&nbsp;<asp:Button ID="btnClear" runat="server" OnClick="btnClear_Click"
                        Text="清空" />
                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="lbtnReg_Click">注册</asp:LinkButton></asp:Panel>
            <asp:Panel ID="Panel2" runat="server" Height="12px" Width="90%" Visible="False">
                <div style="text-align:left;background:dark