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?