Set Master page content from one content page

138 views Asked by At

I have my master page. I need to assign the logged user from the content page. I am using the following code from the landing page

 var currentUser = Membership.GetUser();
 Label lblLoggedUser = (Label)Master.FindControl("lblLoggedUser");
 lblLoggedUser.Text = currentUser.UserName;

But It goes off while I redirect to another page. How can I set the master page content from one single page ?

PS- I am using membership providers for teh log in.

1

There are 1 answers

0
Gridly On BEST ANSWER

I believe what you are saying is that when the user visits the landing page then your listed code executes and sets the user name into the master page. But when they leave the landing page the user name becomes blank. You are trying to set the value once into the master page from the landing page and have it remain for subsequent pagas.

A better approach it to just have the master page set that value. Having the master page be responsible for master page controls frees the individual pages to focus on just their logic.

Move the code into the master page's Page Load or PreRender event.

    void Page_PreRender(object sender, EventArgs e)
    {
        var currentUser = System.Web.Security.Membership.GetUser();
        if (currentUser != null && currentUser.IsOnline)
            this.lblLoggedUser.Text = currentUser.UserName;
        else
            this.lblLoggedUser.Text = "not authenticated";

    }