如何将值从母版页传递到内容页 ASP.Net C#
How to pass value from masterpage to contentpage ASP.Net C#
我的母版页中有一个登录按钮,成功登录后,我希望将我的 loggedUserName 的值传递到我的内容页面,非常感谢帮助和建议!
参考了下面的论坛,不知如何继续:
http://forums.asp.net/t/1758733.aspx?Passing+Value+from+Master+page+to+Content+Page
已编辑:(下面是我在主页中的代码)
public partial class MasterPage : System.Web.UI.MasterPage
{
SqlCommand SQLSelect = new SqlCommand();
SqlConnection SQLCon = new SqlConnection();
DataTable dt = new DataTable("Customer");
int len;
protected void Page_Load(object sender, EventArgs e)
{
SQLCon.ConnectionString = ConfigurationManager.ConnectionStrings["SDMConnectionString"].ConnectionString;
SQLCon.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customer", SQLCon);
len = da.Fill(dt);
string checking = Request.QueryString["user"];
if (checking != null)
{
memberview.Visible = true;
userview.Visible = false;
}
else
{
userview.Visible = true;
memberview.Visible = false;
}
lblLoggedUser.Text = "(" + checking + ")";
}
protected void LoginButton_Click(object sender, EventArgs e)
{
string username = UserName.Text;
string pass = Password.Text;
int counter = 0;
string content = @"category.aspx?content=" + username;
foreach (DataRow row in dt.Rows)
{
string usernameCheck = row["Username"].ToString();
string passCheck = row["Pass"].ToString();
if (username == usernameCheck && pass == passCheck)
{
counter = 1;
Response.Redirect(content);
break;
}
else
{
counter = 0;
}
}
if (counter == 1)
{
Session["user"] = username;
lblLoggedUser.Text = Session["user"].ToString();
}
else
{
HttpContext.Current.Response.Write("<script>alert('Error Username or Password!');</script>");
}
}
}
一种方法是在页面上使用 master
参数并从母版页读取任何内容 public
。这是一个例子
主页
public partial class cMasterPage : System.Web.UI.MasterPage
{
public string getUserName
{
get
{
return "what ever";
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
页数
public partial class cPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cGetValue = ((cMasterPage)Master).getUserName;
}
}
我的母版页中有一个登录按钮,成功登录后,我希望将我的 loggedUserName 的值传递到我的内容页面,非常感谢帮助和建议!
参考了下面的论坛,不知如何继续: http://forums.asp.net/t/1758733.aspx?Passing+Value+from+Master+page+to+Content+Page
已编辑:(下面是我在主页中的代码)
public partial class MasterPage : System.Web.UI.MasterPage
{
SqlCommand SQLSelect = new SqlCommand();
SqlConnection SQLCon = new SqlConnection();
DataTable dt = new DataTable("Customer");
int len;
protected void Page_Load(object sender, EventArgs e)
{
SQLCon.ConnectionString = ConfigurationManager.ConnectionStrings["SDMConnectionString"].ConnectionString;
SQLCon.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customer", SQLCon);
len = da.Fill(dt);
string checking = Request.QueryString["user"];
if (checking != null)
{
memberview.Visible = true;
userview.Visible = false;
}
else
{
userview.Visible = true;
memberview.Visible = false;
}
lblLoggedUser.Text = "(" + checking + ")";
}
protected void LoginButton_Click(object sender, EventArgs e)
{
string username = UserName.Text;
string pass = Password.Text;
int counter = 0;
string content = @"category.aspx?content=" + username;
foreach (DataRow row in dt.Rows)
{
string usernameCheck = row["Username"].ToString();
string passCheck = row["Pass"].ToString();
if (username == usernameCheck && pass == passCheck)
{
counter = 1;
Response.Redirect(content);
break;
}
else
{
counter = 0;
}
}
if (counter == 1)
{
Session["user"] = username;
lblLoggedUser.Text = Session["user"].ToString();
}
else
{
HttpContext.Current.Response.Write("<script>alert('Error Username or Password!');</script>");
}
}
}
一种方法是在页面上使用 master
参数并从母版页读取任何内容 public
。这是一个例子
主页
public partial class cMasterPage : System.Web.UI.MasterPage
{
public string getUserName
{
get
{
return "what ever";
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
页数
public partial class cPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cGetValue = ((cMasterPage)Master).getUserName;
}
}