1

I am trying to set the visibility of a control from an aspx page from the codebehind file, based on a page property set in EPiServer. The relevant control is coded like this:

<asp:Content ContentPlaceHolderID="RelatedContent" runat="server">
    <p id="DateProp" runat="server" Visible ="<%# DateVisible %>"><strong>Date:</strong> <%= ((DateTime)CurrentPage["EventDate"]).ToString("d MMMM yyyy") %></p>
</asp:Content>

I have tried a couple of different ways to do it, based on this previous question, but I haven't been able to get it to work. First, I tried this:

public partial class Event : EPiServer.TemplatePage<EventPage>
    {
        public bool DateVisible = (bool)CurrentPage["HideDate"] == true ? false : true;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind(); 
        }
    }

which gave the error "Error 80 An object reference is required for the non-static field, method, or property EPiServer.PageBase<JamesTrustWF.Web.Models.Pages.EventPage>.CurrentPage.get'"

Then I tried this:

public partial class Event : EPiServer.TemplatePage<EventPage>
{
    public bool DateVisible = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        DateVisible = (bool)CurrentPage["HideDate"] == true ? false : true;
        DataBind(); 
    }
}

which gave the error "Object reference not set to an instance of an object". Any idea how to get it to work?

Community
  • 1
  • 1
rudivonstaden
  • 7,675
  • 5
  • 26
  • 41

1 Answers1

2

The reason that your first example failed is simply because you cannot reference other instance members when initializing an instance field as this is done (just) before the constructor is called, see http://msdn.microsoft.com/en-us/library/ms173118.aspx

The key thing in this case is that properties in EPiServer will return null if the are considered empty. This means that a string property with an empty string value will return null and a Boolean property with a false value also returns null.

So in your case, when you want to assign a Boolean property a value, simply check if the property return any value.

DateVisible = CurrentPage["HideDate"] == null;

or another option is to use the IsValue method on the PageBase class to do the check.

DateVisible = !IsValue("HideDate");

Also, since you are using a strongly typed page type, you can just use the property on the current page (assuming that the HideData property has been added this way)

DateVisible = !CurrentPage.HideDate;

Finally, you could also assign the visible property directly from your code behind without having to go through the DateVisible field and DataBind.

public partial class Event : EPiServer.TemplatePage<EventPage>
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateProp.Visible = !CurrentPage.HideDate;
    }
}
Henrik N
  • 529
  • 4
  • 12
  • Thanks @HenrikN, I had first tried the `DateProp.Visible = !CurrentPage.HideDate` approach, but the Visual Studio editor indicated that `The name 'DateProp' does not exist in the current context`. I assumed that it was because it was inside a ContentPlaceHolder and I'd have to use convoluted FindControl methods. I tested it now though, and it compiled fine, so that would be the preferred approach. Your other suggestions also worked though. – rudivonstaden May 15 '14 at 05:33