Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > displaying a link in a custom doc type View modes: 
User avatar
Member
Member
dcollins-marketwired - 1/28/2014 9:16:54 AM
   
displaying a link in a custom doc type
My doctype displays, in addition to name and title, a "View Video" link video and a "Download Presentation" link.

For some reason, the URLs are coming back as empty (though, mysteriously, not equal to "").

This is how I'm trying to transform them:
  <% if ( Eval("Download_Presentation") != "" ) { %>
<div class="speaker-presentation-link"><a href="<%# Eval("Download_Presentation") %>">> Download Presentation</a>
</div>
<% } %>

i.e. if a URL was specified, display it in the form <a href="myUrl">> Download Presentation</a>, otherwise do not display.

So, problems:
1] I always get <a href=""> an empty URL
2] Even though it seems to resolve to "", the condition testing it always returns true.

There are almost two dozen methods that deal with URLs, but most of them seem to either return the currently displayed document, or require multiple input parameters. Which one simply gives me the URL of the document I've specified?

User avatar
Member
Member
dcollins-marketwired - 1/28/2014 9:39:29 AM
   
RE:displaying a link in a custom doc type
Upon rereading, it looks like I might be more concerned about the conditional. no, that's secondary. My biggest concern is how to get the URL back at all. I can't find any methods that do that.

User avatar
Certified Developer 10
Certified Developer 10
josha-bpstudios - 1/28/2014 9:46:05 AM
   
RE:displaying a link in a custom doc type
This is an example that you can use to base your transformation off of:

First checking a value(Querystring) then returning text. Plug your items into this and it should work. You can put the entire div into the return statement and it should work. You may have to escape some qoutes and make minor changes.

{%if (QueryString.GetValue("createNewAccount") == null) {return "Please Sign In" } else {return "Join Now"}%}

User avatar
Member
Member
dcollins-marketwired - 1/28/2014 9:54:10 AM
   
RE:displaying a link in a custom doc type
josha-bpstudios wrote: This is an example that you can use to base your transformation off of:

First checking a value(Querystring) then returning text. Plug your items into this and it should work. You can put the entire div into the return statement and it should work. You may have to escape some qoutes and make minor changes.

{%if (QueryString.GetValue("createNewAccount") == null) {return "Please Sign In" } else {return "Join Now"}%}

I'm confused. What does Querystring have to do with this?

User avatar
Member
Member
dcollins-marketwired - 1/28/2014 10:37:53 AM
   
RE:displaying a link in a custom doc type
It is frustrating trying to find documentation on this.

What language EXACTLY am I writing in when I do this:
<% if ( Eval("Download_Presentation") != "" ) { %>
I'd think it's ASP (or ASPX, or ASP.NET or C#), but I've tried every version of string comparison in each language and they all throw errors.
<%# Eval("Download_Presentation").Length %>
<%# Len(Eval("Download_Presentation")) %>
<%# Eval("Download_Presentation").IsNullOrEmpty %>

User avatar
Member
Member
dcollins-marketwired - 1/28/2014 3:53:55 PM
   
RE:displaying a link in a custom doc type
OK, things are becoming clear now. :)

It is obviously C#ish but it is a crippled subset of it. That's why not all functions work, and why they've provided a set of methods in place of the missing ones.

The functionality I was looking for is in a premade method called IfEmpty().

<%# IfEmpty(Eval("DownloadPresentationLink"), "", "<div class='speaker-video-link'>
<a href=\""+Eval("DownloadPresentationLink")+"\">> Download Presentation</a>
</div>") %>

So, if the Link is "Empty", my code will not output the div, the anchor or its text at all.


Whew!

I gotta say, i really dislike being forced to use the one-line if-then-else shorthand syntax: (If(condtion, returnIfFalse, returnIfTrue). Very hard to read, hard to debug and - worst of all - forces me to use escaped quotes. :P

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/29/2014 2:44:42 AM
   
RE:displaying a link in a custom doc type
Hello,

When editing transformation, there is a link to list of the available transformation functions mostly used in the ASCX transformations. Usually placed locally at ~/CMSHelp/newedit_transformation_methods.htm.
In TXT or HTML transformations you can also use the K# macros. They are not supported in ASCX transformations - you need to create either custom function, use SCRIPT tags to enter classic C# code or call the ResolveMacros method to resolve the macros.

Best regards,
Juraj Ondrus

User avatar
Member
Member
dcollins-marketwired - 1/29/2014 8:03:22 AM
   
RE:displaying a link in a custom doc type
kentico_jurajo wrote:
When editing transformation, there is a link to list of the available transformation functions mostly used in the ASCX transformations. Usually placed locally at ~/CMSHelp/newedit_transformation_methods.htm.

Yes, I am aware of that page with the methods. It took me some digging to figure out that I can;t just use any ol' C# functions (such as .Length), and how I could get around that with the providued methods.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 1/29/2014 8:18:00 AM
   
RE:displaying a link in a custom doc type
dcollins-marketwired wrote: It took me some digging to figure out that I can;t just use any ol' C# functions (such as .Length), and how I could get around that with the providued methods.
You can use standard C# methods and functions without a problem. I think you might be experiencing issues with this
<%# Eval("Download_Presentation").Length %>
This isn't really a strong way of getting a length especially if you're unsure of the data type that's being returned. The method Eval("") returns an object. So you need to cast that object to a string before getting the length.

This might be a better option
<%# Eval("Download_Presentation").ToString().Length %>
Or even better yet, use the built-in Kentico methods and functions
<%# EvalText("Download_Presentation").Length %>
You could even use
<%# Convert.ToString(Eval("Download_Presentation")).Length %>
You can even use this
<%# string.IsNullOrEmpty(EvalText("Download_Presentation")) ? "some true value" : "some false value" %>
The good and bad is there are about 15,000 different ways to do the same thing with asp.net (or Microsoft products in general) so pick your poison.

Good luck
Brenden

User avatar
Member
Member
dcollins-marketwired - 1/29/2014 8:28:07 AM
   
RE:displaying a link in a custom doc type
FroggEye wrote: You can use standard C# methods and functions without a problem.
Hm. My experience strongly indicates that is not the case, but I will look into it further.

FroggEye wrote:
So you need to cast that object to a string before getting the length.
<%# Eval("Download_Presentation").ToString().Length %>

No, I definitely tried that, exactly as you showed, including the ToString(). It throws an error anyway.

User avatar
Member
Member
dcollins-marketwired - 1/29/2014 8:44:11 AM
   
RE:displaying a link in a custom doc type
OK, so I did some experiments with the premise that you are correct, and any C# code can be used.

This does work:

[<%# Eval("DownloadPresentationLink").ToString().Length > 0 %>]

returning [true] or [false] correctly.

This, however, does not work:

<% if( Eval("DownloadPresentationLink").ToString().Length > 0 ){ %>
foo
<% } %>

It explodes the whole page with the error "Object reference not set to an instance of an object."

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 2/3/2014 2:16:45 AM
   
RE:displaying a link in a custom doc type
Hi,

It seems that the content of the Eval function is not resolved at given time when the IF statement needs it. I would recommend creating a custom transformation function for this purpose. I am sorry for this inconvenience.

Best regards,
Juraj Ondrus

User avatar
Member
Member
dcollins-marketwired - 2/3/2014 8:21:31 AM
   
RE:displaying a link in a custom doc type
kentico_jurajo wrote: I would recommend creating a custom transformation function for this purpose.

There is no need fior a custom function. This works:
IfEmpty(Eval("DownloadPresentationLink")

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 2/4/2014 1:57:07 PM
   
RE:displaying a link in a custom doc type
dcollins-marketwired wrote: This, however, does not work:

<% if( Eval("DownloadPresentationLink").ToString().Length > 0 ){ %>
foo
<% } %>

It explodes the whole page with the error "Object reference not set to an instance of an object."
This is not standard asp.net code which is why it is erroring out. You do not include the "if" in your transformation. You simply do
<%# (Eval("DownloadPresentationLink").ToString().Length > 0) %>


User avatar
Member
Member
dcollins-marketwired - 2/4/2014 2:57:43 PM
   
RE:displaying a link in a custom doc type
FroggEye wrote:
dcollins-marketwired wrote: This, however, does not work:

<% if( Eval("DownloadPresentationLink").ToString().Length > 0 ){ %>
foo
<% } %>

It explodes the whole page with the error "Object reference not set to an instance of an object."
This is not standard asp.net code which is why it is erroring out. You do not include the "if" in your transformation. You simply do
<%# (Eval("DownloadPresentationLink").ToString().Length > 0) %>


OK, there's some confusion here. When you say ' You do not include the "if" in your transformation. You simply do... ' it is ambiguous whether you are saying that is what I AM doing and it's wrong, or that is what I SHOULD be doing, but am not.

Regardless, I see nothing wrong with my code or logic.

This:
<%# Eval("DownloadPresentationLink").ToString().Length > 0 %>
will directly output true or false.

If I put it in an if statement, like so:
<%if ( Eval("DownloadPresentationLink").ToString().Length > 0 %> ) {}

There is no syntactical reason why it should not work.

User avatar
Member
Member
dcollins-marketwired - 2/4/2014 3:00:20 PM
   
RE:displaying a link in a custom doc type
Oops. Typo in that last line of sample code. It should say:

<% if( Eval("DownloadPresentationLink").ToString().Length > 0 ){ %>