Question

I've got an ASP.net application that is used to display information queried from our ERP system onto various web pages. The main object is called an EpicorUser, and it basically encapsulates all current information about an employee.

This object is used to fill in a bunch of various fields on the master page such as Full Name, current activity, clock in/out times, etc.

I am trying to pass this object from the MasterPage into the content pages to avoid needlessly querying the WebService that serves this information. The problem is, when I access the object from a ContentPage, it is always null. I know it has been populated because my MasterPage content is all filled in correctly.

I am trying to access the MasterPage 'CurrentUser' object from my ContentPage like this:

**MasterPage Codebehind:**
public EpicorUser CurrentUser; //This object is populated once user has authenticated

///This is in my ContentPage ASPX file so I  can reference the MasterPage from codebehind
<%@ MasterType VirtualPath="~/Pages/MasterPage/ShopConnect.Master" %>

**ContentPage CodeBehind:**
string FullName = Master.CurrentUser.UserFileData.FullName; //CurrentUser is null(but it shouldn't be)

Strange thing is, I had another content page where this system worked fine. It has also stopped working, and I don't think I have changed anything on the masterpage that could cause this. I had set the CurrentUser as a public property so I could access

I went as far a creating a method to re-populate the object from the master page, and calling it from the code-behind on the contentpage:

**ContentPage code-behind:**
EpicorUser CurrentUser = Master.GetCurrentUserObject();

**MasterPage Method being invoked:**
public EpicorUser GetCurrentUserObject()
{
    using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "OFFICE"))
    {
        UserPrincipal principal = UserPrincipal.FindByIdentity(context, HttpContext.Current.User.Identity.Name);
        EpicorUser CurrentEmployee = RetrieveUserInfoByWindowsID(principal.SamAccountName);   
        return CurrentUser; //Object is NOT null before the return
    }
}

**ContentPage code-behind return:**
EpicorUser CurrentUser = Master.GetCurrentUserObject(); //But object is now null once we return  

Stepping through the code shows me that the CurrentUser object is populated correctly in the MasterPage code behind, but once it is returned to the ContentPage code behind, it is now null!

Anyone know where the disconnect is?

Was it helpful?

Solution

Content Page is loaded first and then Master page will be loaded. So, your property could be blank when it is accessed in the content page. You can try creating a public method(to return UserObject) on the master page and then call the method from content page.

Another option is creating a base page class(inherit all content pages) and create a property to return the user object. So, all pages can access the value

EDIT:

public class BasePageClass : System.Web.UI.Page
{
    public List<string> LookupValues
    {
        get
        {
            if (ViewState["LookupValues"] == null)
            {
                /*
                    * create default instance here or retrieve values from Database for one time purpose
                    */
                ViewState["LookupValues"] = new List<string>();
            }
            return ViewState["LookupValues"] as List<string>;
        }
    }
}
public partial class WebForm6 : BasePageClass
{
    protected void Page_Load(object sender, EventArgs e)
    {           
    }
    protected void MyButton_Click(object sender, EventArgs e)
    {
        //access lookup properties
        List<string> myValues = LookupValues;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top