当我登录时,我不想在下一页显示用户名,我该怎么做 [英] when i login i wnt to show username in next page how can i do this

查看:58
本文介绍了当我登录时,我不想在下一页显示用户名,我该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码...第一页


this is my code...1st page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection("Data Source=.......;Database=........;User ID=usr_ima;password=temp123");
        

    protected void Page_Load(object sender, EventArgs e)
    {
    
    }

    protected void btnlogin_Click(object sender, EventArgs e)
    {
        con.Open();

        SqlCommand cmd = new SqlCommand("select * from tbluser where username=@username and userpassword=@userpassword", con);
        cmd.Parameters.AddWithValue("@username", TextBox1.Text);
        cmd.Parameters.AddWithValue("@userpassword", TextBox2.Text);
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Session["username"] = "";

            //Session["New"] = dt;
            Response.Redirect("Default3.aspx");
        }
        else
        {
            Label3.Visible = true;
            Label3.Text = "Invalid username&password....!!!";
        }

    }
}


第二页..



2nd page..


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["username"].ToString();
        lblUserName.Text = Session["username"].ToString();
    }
}

推荐答案

hi 我相信您的代码很好,
只需检查会话是否为空
hi i believe ur code is fine ,
just check if the Session is NULL


您可以做一件事,成功登录后,您应该创建已经创建的Session:
You can do one thing, On Successful login you should create Session which you have already created:
if(compare user name and password)
{
     //if credentials matches
     Session["UName"]=txtUName.text;
     Response.Redirect("Home.aspx");
}


在母版页的Page_Load()事件中检查会话
Page_Load(.... ....)


In the Page_Load() event of the Master page check for the session
Page_Load(.... ....)

If(Session["UName"]==null)
       {
               Response.Redirect("user_Login.aspx");
       }
       else
       {
              lblUName.text=Session["UName"].ToString();
       }


检查此&让我知道您是否遇到任何问题:)


Check this & let me know if you face any problem :)


除此之外,您还可以参考下面的链接,该链接讨论了ASP.NET中的状态管理

初学者ASP.NET中的状态管理技术简介

满足此要求最常用的方法是在代码中使用的会话状态.
In addition to that you can also refer to the link below that discuss about the State management in ASP.NET

Beginners Introduction to State Management Techniques in ASP.NET

The most used way for this requirement is Session state as you have used in your code.


这篇关于当我登录时,我不想在下一页显示用户名,我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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