Reputation: 13
I'm using Sitecore and just getting the hang of working within ASP.NET. We don't have a second Sitecore license for a development server, so I have to do everything live fire on the site (ack!), so I am trying to avoid working with code behinds due to the necessity of a recompile/DLL insert.
I'm just trying to hide a section header if the following section is empty. I've come up with this, which definitely works, but it seems pretty bulky:
<% if (!string.IsNullOrEmpty(Sitecore.Context.Item.Fields["Grades"].ToString())) { %><h2 class="edu">Timeframe</h2><% } %>
<sc:FieldRenderer runat="server" ID="mhTimeFrame" Fieldname="Timeframe" />
Is there a more straightforward way to do this?
By the way: I'm aware that Sitecore can utilize XSLT templates but our site was built without utilizing XSLT so I'd like to stick to one paradigm so that a future developer can make sense of this.
Upvotes: 0
Views: 138
Reputation: 2127
In order to get rid of the if
-statement in your markup, you can set the visible attribute of your <h2 />
element:
<h2 class="edu" runat="server" Visible='<%# Sitecore.Context.Item.Fields["Grades"] != null %>'>
Timeframe
</h2>
To get this up and running, you need to trigger the DataBinding at least one time:
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
Nevertheless you NEED a development environment ;)
Upvotes: 0
Reputation: 4008
Just to state the obvious, you need to get a proper development process in place or you will get yourself into trouble! If you haven't already, talk to Sitecore and figure out what you need in terms of licenses to get a proper development environment up and running. You may be entitled to a development instance if you are a certified developer.
Now, to your question, you have to put the logic somewhere. If you are unable to modify the codebehind, compile and deploy then you need to put it on the .ascx. You can trim it up a bit I suppose...
<% if (Sitecore.Context.Item["Grades"] != "") { %><h2 class="edu">Timeframe</h2><% } %>
<sc:FieldRenderer runat="server" ID="mhTimeFrame" Fieldname="Timeframe" />
Upvotes: 3