如何获取会话值并分配或显示在标签上 [英] how to get the session value and assign or shown on label

查看:63
本文介绍了如何获取会话值并分配或显示在标签上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取会话值并分配给标签我有以下代码



how to get the value of session and assign to a label me have the following code

private void Login()
    {
        //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
        //SqlConnection con = new SqlConnection("Server=192.168.0.110;Database=PRODUCT;User Id=sa;Password=abc@123");
        //con.Open();
        //Response.Write("<script>alert('suc')</script>");
        if (txtUserName.Text == "" & txtPassword.Text == "")
        {
            lblMsg.Text = "User ID and Password is Required!";
            //return;
        }
        else
        {
            con.Open();
            SqlCommand com =
            new SqlCommand(
                "select * from tbl_Login where UserName='" + txtUserName.Text + "' and UserPassword='" + txtPassword.Text + "'"
                , con);
            com.CommandType = CommandType.Text;
            SqlDataReader DR;

            DR = com.ExecuteReader();


            if (DR.Read())
            {
                Session["Name"] = txtUserName.Text.ToString();
                Session["DEPTID"] = txtPassword.Text.ToString();
                if (txtUserName.Text != "admin" & txtPassword.Text != "admin")
                {       //how to show the Session Name on lblUserName            
                    //lblUserName.Text = Session["Name"].ToString();
                    Response.Redirect("AddItems.aspx");
                }
                else
                    Response.Redirect("frmLogin.aspx");
            }
            else
            {
                lblMsg.Text = "Invalid Cridential!";
            }
            con.Close();
        }

    }

    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        try
        {
            Login();
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }

推荐答案

试试这个。



Label1.Text = Session [Name]。Tostring();
Try this.

Label1.Text = Session["Name"].Tostring();


这篇关于如何获取会话值并分配或显示在标签上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆