【问题标题】:How to pass session in asp.net如何在asp.net中传递会话
【发布时间】:2016-03-12 08:12:06
【问题描述】:

我想使用会话将用户名从登录页面传递到母版页,但我收到“对象引用未设置为对象实例”错误。我的登录页面代码是,

protected void btnLogin_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    SqlConnection con = new SqlConnection(connectionString);
    con.Open();

    SqlCommand cmd = new SqlCommand("select * from Registration where UserName = @username and Password = @password", con);
    cmd.Parameters.AddWithValue("@username", txtLogin.Text);
    cmd.Parameters.AddWithValue("@password", txtPassword.Text);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        Session["name"] = txtLogin.Text.Trim();
        Response.Redirect("ImageSearch.aspx");          
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }         
}

对于母版页代码,

protected void Page_Load(object sender, EventArgs e)
{       
        Label1.Visible = true;
        string sessionUserName = (string)(Session["name"]);
        Label1.Text = sessionUserName;      
}

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    根据 Asp.net 中的页面生命周期,Page_Load 总是在事件之前触发。 在您的情况下,您的登录页面的 Page_Load 首先是触发,您的母版页的 Page_load,然后是事件点击。这就是为什么您的母版页中的 Session["Name"] 为空。

    如果您想在单击后立即更新母版页中的“Label1.Text”,则需要手动更新母版页。

    protected void btnLogin_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
    
            SqlCommand cmd = new SqlCommand("select * from Registration where UserName = @username and Password = @password", con);
            cmd.Parameters.AddWithValue("@username", txtLogin.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
    
    
                Session["name"] = txtLogin.Text.Trim();
                (Master as SiteMaster).UpdatePage();
                Response.Redirect("ImageSearch.aspx");
    
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
            }
    
        }
    

    然后在您的母版页中,添加一个方法:

        public void UpdatePage()
        {
            Label1.Visible = true;
            string sessionUserName = (string)(Session["name"]);
            Label1.Text = sessionUserName;
        }
    

    【讨论】:

      【解决方案2】:

      母版页加载事件将在按钮单击事件之前执行,因此在这种情况下您不会在母版页加载时获得会话值。

      https://msdn.microsoft.com/en-us/library/dct97kc3.aspx

      您可以尝试从按钮单击事件本身设置母版页标签值 -

      this.Master.Label11.Text = txtLogin.Text.Trim();
      

      【讨论】:

        猜你喜欢
        • 2020-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 2015-05-29
        相关资源
        最近更新 更多