我创建了简单的登录页面,但是我遇到了登录用户ID的问题
public partial class LoginwithEncryption : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(
"select * from dbo.UserInfo where Login =@Login and Password=@Password", con);
cmd.Parameters.AddWithValue("@Login", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("StartPage.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation",
"<script language='javascript'>alert('Invalid UserName and Password')</script>");
}
}
}
登录后如何获取用户ID(我能登录)?我尝试了几种方法,但它不起作用;(
我的数据库:
答案 0 :(得分:4)
您可以将用户名存储在Session
变量中。例如:
if (dt.Rows.Count > 0)
{
//Store username in session
Session["UserName"] = txtUserName.Text;
Response.Redirect("StartPage.aspx");
}
然后您可以在以下页面上检索它:
if (Session["UserName"] != null)
{
Literal1.Text = (string)Session["UserName"];
}
答案 1 :(得分:2)
根据您提供的信息,您只需从包含登录用户的数据表中提取行数据。
例如:
//Extract data
User objUser = new User();
objUser.Id = int.parse(dt.Rows[0]["ID"].ToString());
objUser.Login = dt.Rows[0]["Login"].ToString();
objUser.Password = dt.Rows[0]["Password"].ToString();
objUser.Type= int.parse(dt.Rows[0]["Password"].ToString());