1

我必须在单击按钮时从内容页面更改母版页上标签中的文本,但这是皱纹......它是在表单登录重定向期间。有没有办法做到这一点?这是我的代码,一切都设置正确我认为问题是重定向将标签更改回默认文本值。

登录代码

    protected void Login_Click(object sender, EventArgs e)
    {


         var result = Utilities.AuthenticateUser(txtUserName.Text, txtPassword.Text);


                if (result.Authenticated)
                {
                    this.User = result;



                    DDC master = (DDC)Page.Master;
                    master.lblLogin_Text = result.FirstName + ' ' + result.LastName;

                    FormsAuthentication.RedirectFromLoginPage(result.Username, false);
                    return;
                }
            }


    }

母版页

public partial class DDC : System.Web.UI.MasterPage
{

    public string lblLogin_Text
    {
        get { return lblLogin.Text; }
        set { lblLogin.Text = value; }

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
4

1 回答 1

1

使用 aSession来跟踪您的身份验证结果。由于您的示例代码中没有类名,因此假设它的AuthenticationResult.

MasterPage在您的登录页面中,进行身份验证,然后设置一个可以读取的会话变量:

protected void Login_Click(object sender, EventArgs e)
{
    var result = Utilities.AuthenticateUser(txtUserName.Text, txtPassword.Text);
    if (result.Authenticated)
    {
        Session["AuthenticationResult"] = result;
        FormsAuthentication.RedirectFromLoginPage(result.Username, false);
    }
}

在您MasterPage的 'sPage_Load中,让它检查Session变量并设置Label文本:

public partial class DDC : System.Web.UI.MasterPage
{
    public string lblLogin_Text
    {
        get { return lblLogin.Text; }
        set { lblLogin.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var result = (Session["AuthenticationResult"] as AuthenticationResult);
            if (result != null && result.Authenticated)
            {
                this.User = result;
                lblLogin_Text = String.Form("{1} {2}, result.FirstName, result.LastName);
            }
            else
            {
                lblLogin_Text = String.Empty;
            }
        }
    }
}
于 2013-08-28T13:38:30.787 回答