If Syntax in transformation

Justin L asked on July 10, 2017 14:03

Hi again Kentico guru's,

I'm trying to make a list of categories and highlight one category if there is one specified in the url querystring (http://example.com/blog.aspx?categoryname=category). I am using the querystring because i also have a repeater that outputs all posts inside the current category which also uses a querystring to keep track of the current page (e.g. http://example.com/blog.aspx?page=2).

Unfortunately i'm running into some difficulties, so far i've found out how to retrieve the query string and made a repeater that retrieves all categories via a custom query. Now i want to create the transformation and show some different things when the category from the database is the same as the one specified in the url (current category).

How can i accomplish this? Currently i have the following code:

<%

var urlParam = Request.QueryString["categoryname"];
var dbField = Eval("CategoryName").ToString().ToLower();

if(urlParam == dbField) { %>
    <a href="#" class="category category--current"><%# Eval("CategoryName") %></a>
<% } else { %>
    <a href="#" class="category"><%# Eval("CategoryName") %></a>
<% } %>

But this returns the following error:

Server Error in '/' Application.

Object reference not set to an instance of an object.

I've tried using the IfCompare method and played around with different opening tags, but i don't really understand the use for each different tag: <% %> or <%# %> or <%= %>

Can you guys please help/explain the differences?

Thanks!

Best regards, Justin

Recent Answers


Trevor Fayas answered on July 10, 2017 15:29

I've always had to store any advanced logic (variables, etc) in a <script runat="server">public bool MyFunction() {} </script> type of way. The normal <%# %> syntax is usually only good for single commands, which honestly i'm not sure why you aren't using. Check Kentico's Transformation method references, otherwise the below is what i would use.

<a href="#" class="category <%# IfCompare(Request.QueryString["categoryname"],Eval("CategoryName").ToString().ToLower(), "", "category--current") %>"><%# Eval("CategoryName") %></a>

If you don't need ASCX, it would be cleaner using Text/XML:

<a href="#" class="category {% QueryString.category == CategoryName.ToLower() ? "category--current" : "" @%}">{% CategoryName %}</a>

2 votesVote for this answer Mark as a Correct answer

Justin L answered on July 10, 2017 20:25 (last edited on July 10, 2017 20:49)

Hi Trevor, thank you for your reply. I need a bit more logic/more advanced if statement, because i have to combine the querystrings of the paginator and the categories. That is the reason why i was playing around with the multiline if statements.

I've managed to solve my issue with the following combination:

<% if(Request.QueryString["pages"] == null) { %>

    <%# IfCompare(Request.QueryString["categoryname"],Eval("CategoryName").ToString().ToLower(), "<a href=\""+CurrentDocument.NodeAliasPath+".aspx?categoryname="+Eval("CategoryName").ToString().ToLower()+"\" class=\"filter__item\">"+Eval("CategoryName")+"</a>", "<a href=\""+CurrentDocument.NodeAliasPath+".aspx\" class=\"filter__item filter__item--active\">"+Eval("CategoryName")+"</a>") %>

<% } else { %>

    <%# IfCompare(Request.QueryString["categoryname"],Eval("CategoryName").ToString().ToLower(), "<a href=\""+CurrentDocument.NodeAliasPath+".aspx?page="+Request.QueryString["pages"]+"&categoryname="+Eval("CategoryName").ToString().ToLower()+"\" class=\"filter__item\">"+Eval("CategoryName")+"</a>", "<a href=\""+CurrentDocument.NodeAliasPath+".aspx?page="+Request.QueryString["pages"]+"\" class=\"filter__item filter__item--active\">"+Eval("CategoryName")+"</a>") %>

<% } %>

There must be a waaay better way to accomplish this, but at least this works :)

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 10, 2017 20:46 (last edited on December 10, 2019 02:31)

I would try doing what you did with macros and Text/XML transformation type, it will reduce the mess that you see a bit, it's a bit cleaner.

You also can pass an empty page querystring and it will default it to the first page, so you can further reduce your transformation in half.

<a href="{% GetDocumentUrl() %}?categoryname={% CategoryName.ToLower() |(identity)GlobalAdministrator%}</a>

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.