I have a class named utility in my App_code folder that holds the logic to set a labels text.
public void Mgr_BreadCrumbs(string text)
{
Manager.MasterPages.Master master = new Manager.MasterPages.Master();
Label lblHeader = (Label)master.FindControl("lblHeader");
lblHeader.Text = text;
}
And then in each of my pages I was trying to set it like this:
utility u = new utility();
u.Mgr_BreadCrumbs("Categories");
I’m getting am object reference not set to an instance of an object on the lblHeader.Text = text; line of code.
That’s because you’re creating a new instance of your master page’s class, but it’s not being initialized, so the label doesn’t have a value.
What you really want to do is probably pass either the current Page or its Master Page into the utility method so that it can find the actual Master Page instance being used by the current page. (Setting a label value on some other instance that you create won’t have any effect on the current page’s state).
…