如何将值从主页面传递到内容页面ASP.Net C#

时间:2016-08-07 07:41:30

标签: c# asp.net session master-pages content-pages

我的主页上有一个登录按钮,在我成功登录后,我希望将 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>");
    }

}

}

1 个答案:

答案 0 :(得分:1)

一种方法是在页面上使用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;
    } 
}
相关问题