protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            panelLogin.Visible = true;
            labHuangYing.Text = "你好,请登陆!";
            panelXinXi.Visible = false;
        }
        else
        {
            panelLogin.Visible = false;
            string userinfo = Session["UserName"].ToString();
            labHuangYing.Text = userinfo;
            panelXinXi.Visible = true;
            
        }
    }
    protected void btnDengLu_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["zyconnection"].ConnectionString);        conn.Open();        SqlCommand CMD = new SqlCommand("select count (*) from tb_user where typeid=1 and username='"+txtUserName.Text+"' and userpassword='"+txtPassWord.Text+"'", conn);        int count = Convert.ToInt32(CMD.ExecuteScalar());        if (txtUserName.Text == "" || txtPassWord.Text == "")
        {
            Response.Write("<script>alert('请输入用户名或密码')</script>");
        }
        else
        {
            if (count == 0)
            {
                Response.Write("<script>alert('用户名或密码错误!')</script>");
            }
            else
            {
                Session["UserName"] = txtUserName.Text;
                Response.Redirect("Default.aspx");
                conn.Close();
            }

解决方案 »

  1.   

    调试一下看看Session["UserName"] 有没有值
      

  2.   

    你代码的逻辑有问题把?
    protected void Page_Load(object sender, EventArgs e) --初始化页面
        { 
            if (Session["UserName"] == null)  如果Session等于null了   提示登陆
            { 
                panelLogin.Visible = true; 
                labHuangYing.Text = "你好,请登陆!"; 
                panelXinXi.Visible = false; 
            } 
            else 
            { 
                panelLogin.Visible = false; 
                string userinfo = Session["UserName"].ToString();
                labHuangYing.Text = userinfo; 
    //如果session不等于空的时候 让labHuangYing.Text 控件显示
                panelXinXi.Visible = true; 
                
            } 
        } 
        protected void btnDengLu_Click(object sender, EventArgs e) 你的登陆事件
        { 
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["zyconnection"].ConnectionString); 
            conn.Open(); 
          数据库连接
            SqlCommand CMD = new SqlCommand("select count (*) from tb_user where typeid=1 and username='"+txtUserName.Text+"' and userpassword='"+txtPassWord.Text+"'", conn);         int count = Convert.ToInt32(CMD.ExecuteScalar());  执行SQL语句 查是否有此用户        if (txtUserName.Text == "" ¦ ¦ txtPassWord.Text == "")  //验证用户控件是否为空(为什么要写在这里呢)
            { 
                Response.Write(" <script>alert('请输入用户名或密码') </script>"); 
            } 
            else 
            { 
                if (count == 0) //判断用户是否存在
                { 
                    //不存在提示用户名称或密码错误
                    Response.Write(" <script>alert('用户名或密码错误!') </script>"); 
                } 
                else 
                { 
                    存在的时候你把用户的UserName 保存给Session  然后转页  这里并没有体现你把Session显示在那里啊?
                    Session["UserName"] = txtUserName.Text; 
                    Response.Redirect("Default.aspx"); 
                    conn.Close(); 
                } 
      

  3.   

    protected void Page_Load
    是你登陸的時候觸發的,
     protected void btnDengLu_Click
    是你Click的時候觸發的,
    但是你顯示是在protected void Page_Load裡面,Click后又沒調用Load,怎么會顯示呢?
    如樓上。
      

  4.   

    Session["UserName"] = txtUserName.Text; 
    你这段程序
    永远都赋值不到Session里
      

  5.   

    我的好使了,3楼正解,我不知道大家。还有我在page-load里写的,页面不是先加载吗?怎么不可以啊?给分