我有三个页面( Home, Register, Store )相互链接。 主页针对保存在数据库中的预先存在的用户提供登录选项。 如果只有登录成功,有人可以访问商店页面,否则点击商店页面什么都不做,(只需坚持在同一主页)。
Home.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)//login { SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\lab1.mdf;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM userdata WHERE username='" + TextBox1.Text + "'"; cmd.Connection = conn; //cmd.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { //Session["name"] = dt.Rows[0]["userName"].ToString(); Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); } else { Response.Redirect("Register.aspx"); } }Home.aspx:
<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>Store.aspx:
<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>I have three pages (Home, Register, Store)linked with each other. The home page offers log in option against a pre existing user saved in database. Someone can visit the Store page if only the log in successful, otherwise clicking in the Store page do nothing, (just stick in the same Home page).
Home.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)//login { SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\lab1.mdf;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM userdata WHERE username='" + TextBox1.Text + "'"; cmd.Connection = conn; //cmd.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { //Session["name"] = dt.Rows[0]["userName"].ToString(); Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); } else { Response.Redirect("Register.aspx"); } }Home.aspx:
<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>Store.aspx:
<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>最满意答案
有两种方法可以创建一个类并定义一个静态字段,另一种方式是session,但我更喜欢session for login
首先,你将创建一个会话,例如
if (dt.Rows.Count > 0) { Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); Session["UserAuthentication"] = true; }现在检查每个页面或者如果你在.cs文件中有母版页添加代码来检查天气UserAuthentication是否为真,并记得将代码添加到cs文件的Page_Load中
加一张支票
if (Convert.ToBoolean(Session["UserAuthentication"]) == false) { Response.Redirect("Register.aspx"); }there are two ways one is to create a class and define a static field and other way is session but i would prefer session for login
first you will create a session e.g
if (dt.Rows.Count > 0) { Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); Session["UserAuthentication"] = true; }and now check on every page or if you have master page in the .cs file add code to check weather UserAuthentication if true or not and remember to add code into the Page_Load of the cs file
add a check
if (Convert.ToBoolean(Session["UserAuthentication"]) == false) { Response.Redirect("Register.aspx"); }更多推荐
发布评论