0

In Master.cs:

public string sortOrder
{
    get
    {
        if (ViewState["sortOrder"].ToString() == "Desc")
        {
            ViewState["sortOrder"] = "Asc";
        }
        else
        {
            ViewState["sortOrder"] = "Desc";
        }

        return ViewState["sortOrder"].ToString();
    }
    set
    {
        ViewState["sortOrder"] = value;
    }
}

I am calling it from a content page:

ViewState["sortOrder"] = "Asc";
PD (e.SortExpression, Master.sortOrder, false);

When executing the function, I get the following error in this line

if (ViewState["sortOrder"].ToString() == "Desc"): Object reference not set to an instance of an object.

The sortOrder function worked fine when it was also in the content page. I am trying to move all my re-usable code to Master.cs file.

How can I modify the function so it works as it was in the content page.

israel altar
  • 1,768
  • 1
  • 16
  • 24
SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122
  • 1
    Related: [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül May 26 '15 at 12:53
  • Is it null because it is in a separate code-behind file? Doesn't `ViewState` interchange between master and content page? – SearchForKnowledge May 26 '15 at 12:54
  • Why you want to send parameter to master page? – Systematix Infotech May 26 '15 at 13:01
  • @SystematixInfotech I use that function across the pages so I would like to use one function and use it everywhere else. My backup option would be to throw it inside a class and then call it. – SearchForKnowledge May 26 '15 at 13:06
  • Your `get` function shouldn't be changing the value of `ViewState["sortOrder"]`. By definition, a get accessor shouldn't change the state of the system. – Chris Walsh May 26 '15 at 13:06
  • Other way to do this is to create one label control and make it invisible on master page and set its text from content page to get sort order – Systematix Infotech May 26 '15 at 13:48

2 Answers2

2

The ViewState of the master and the content pages are not the same. On your content page, you are only modifying the ViewState of the content page, so your master page is getting the null reference as a result.

To persist data between the master and content pages, you have a few options:

  • If the data needs to persist across postbacks, then either use Session or reference the ViewState of the master through ((MasterType)Page.Master).sortOrder on Content code behinds
  • Otherwise, you can use the Context.Items object.
Jason W
  • 13,026
  • 3
  • 31
  • 62
  • `MasterType` is? I think that might work except I would like to know what `MasterType` is. Thank you :) – SearchForKnowledge May 26 '15 at 13:05
  • 1
    It is the type of your specific master page object. The `Page.Master` property of the content page references the master page, but it is of type MasterPage, which won't have your custom property in it. You have to cast the Master property to the type of your specific master page, or set the MasterType on your content page (https://msdn.microsoft.com/en-us/library/vstudio/ms228274%28v=vs.100%29.aspx) – Jason W May 26 '15 at 13:07
  • I tried, `this.Master.sortOrder` but that didn't work. I also tried this: `((Site)Page.Master).sortOrder` and I got the same result. – SearchForKnowledge May 26 '15 at 13:12
  • Try `((Master)this.Master).sortOrder`. You have to cast it to your master type, and I'm assuming since you posted "Master.cs" that the type of your master page is `Master`. This assumes the Master namespace is accessible to the content page. Alternatively just set the MasterType You could also set the master type with something like `<%@ MasterType VirtualPath="~/Master.master"" %>` setting the VirtualPath to the location of your master within the source of your content pages. – Jason W May 26 '15 at 13:15
  • My content page has this: `<%@ MasterType TypeName="Ob.Pages.Site" %>` at the top and I used the line I mentioned above and I am getting the error. How would I use the `Context.Items` otherwise? – SearchForKnowledge May 26 '15 at 13:17
1

try to use this one.

public string sortOrder
{
      get
      {
           if (!string.IsNullOrEmpty(Convert.ToString(ViewState["sortOrder"])) && Convert.ToString(ViewState["sortOrder"]) == "Desc")
           {
                ViewState["sortOrder"] = "Asc";
           }
           else
           {
                ViewState["sortOrder"] = "Desc";
           }

           return Convert.ToString(ViewState["sortOrder"]);
      }
      set
      {
           ViewState["sortOrder"] = value;
      }
}