【发布时间】:2014-06-09 05:46:09
【问题描述】:
我在 asp.net 中使用 单个母版页 来实现登录和注销功能... 但在母版页中 会话名称 采用空值。 这是我的代码,请帮助我... MasterPage.master.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["name"] == null)
{
Panel2.Visible = false;
Panel1.Visible = true;
}
else if (Session["name"] != null)
{
Panel1.Visible = false;
Panel2.Visible = true;
Label2.Text = "WELCOME | Mr." + Session["name"].ToString();
}
}
}
protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
{
Session.Clear();
Session.Abandon();
}
我的homepage.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string st="select Label4,Label3 FROM Register1_master WHERE Label4='" + TextBox1.Text + "' and Label3='" + TextBox2.Text + "'";
cmd = new SqlCommand(st, sqlcon);
cmd.Connection.Open();
string result= null;
Object value=cmd.ExecuteScalar ();
if ( value != null)
{
result = value.ToString ();
Session["name"] = TextBox1.Text;
Response.Redirect("Main.aspx");
}
else
{
Label3.Text="Invalid username or password";
}
cmd.Connection.Close();
}
从主页登录后,我将进入 Main.aspx 页面
我的 Main.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HyperLink link = (HyperLink)Master.FindControl("HyperLink1");
link.Visible = false;
HyperLink link1 = (HyperLink)Master.FindControl("HyperLink2");
link1.Visible = true;
Label masterlbl = (Label)Master.FindControl("Label2");
string login = Convert.ToString(Session["name"]);
Session["name"] = login;
}
}
【问题讨论】:
-
F9 on --> Session["name"] = TextBox1.Text; F9 也在 --> Session["name"] = login;你确定你打了这个声明?价值观是什么?
-
您确定在您的项目中启用了会话状态吗?见:msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.85).aspx
-
请使用 string st="select * FROM Register1_master WHERE Label4='" + TextBox1.Text + "' and Label3='" + TextBox2.Text + "'";
-
实际上我这样做了...但是 session["name"] 给出了一个空值。我认为在单个母版页中,这是非常典型的... Infer-On
-
In line 'string st="select Label4,Label3 FROM Register1_master WHERE Label4='" + '....你确定“+”合适吗?
标签: c# asp.net session hyperlink master-pages