Recommended way of adding conditional hyperlink to widget template
I have some lists that I am creating widget templates for and have added some custom fields to them.
One of these custom fields is something I am calling GoogleMapUrl. What I want to do is only render it if there is a value. Right now I am able to pull it out with simple markup like
<
a
id
=
"GoogleMapUrl"
href
=
'<% #Eval("GoogleMapUrl")'
>Map</
a
>
<
sitefinity:TextField
runat
=
"server"
DisplayMode
=
"Read"
Value='<%# Eval("GoogleMapUrl")%>' WrapperTag="A" Title="Map" />
Hello,
The most straightforward solution would be to use an external template with codebehind part, however if you're looking for a markup only solution, you can try out a solution similar to the one outlined in this forum thread:
<
asp:TemplateField
HeaderText
=
"hyperlink"
SortExpression
=
"hyperlink"
>
<
ItemTemplate
>
<
asp:HyperLink
NavigateUrl='<%# Eval("hyperlink","http://0")
%>' Visible='<%# (Eval("hyperlink")==DBNull.Value ? false : true)
%>' Text="Go to Web Page" runat="server" /> </
ItemTemplate
>
</
asp:TemplateField
>
'<%# Eval("GoogleMapUrl")%>'
or not.I'd love it if this actually worked. I am trying this in the News List template:
<
h2
class
=
"sfnewsTitle"
>
TRLink is "<%# Eval("ExternalLink").ToString() %>": '<%# ((Eval("ExternalLink").ToString() == "") ? "nothing" : "something") %>' <
br
/>
Internal <
br
/>
<
sf:DetailsViewHyperLink
Target
=
"_blank"
ID
=
"DetailsViewHyperLink1"
TextDataField
=
"Title"
ToolTipDataField
=
"Description"
runat
=
"server"
Visible='<%# ((Eval("ExternalLink")==DBNull.Value || Eval("ExternalLink") == "") ? true : false) %>' /> <
br
/>
External <
br
/>
<
asp:HyperLink
Target
=
"_blank"
NavigateUrl='<%# (Eval("ExternalLink")==DBNull.Value ? Eval("ItemDefaultUrl") : Eval("ExternalLink")) %>' Visible='<%# (Eval("ExternalLink")==DBNull.Value ? false : true) %>' Text='<%# Eval("Title")%>' runat="server" /> <
br
/>
</
h2
>
This is what I ended up using that worked for me.
<
asp:HyperLink
NavigateUrl='<%# Eval("GoogleMapUrl")%>' Visible='<%# string.IsNullOrEmpty(Eval("GoogleMapUrl").ToString()) ? false : true %>' Text="Directions to It!" runat="server" />
Hey Stacey, thanks for taking the time.
I'm trying what you've suggested by doing a little test case as follows:
'<%# string.IsNullOrEmpty(Eval("ExternalLink").ToString()) ? "nothing" : "something" %>' <
br
/>
Hello everyone,
As far as I understood you want to remove the label in front of the value, if the value (of the custom field) is null? Please correct me if I'm wrong. I have prepared a small jQuery sample on how this can be achieved.You can place the code in a javascript widget and configure the widget to put the code "before the closing body tag".Here is the markup in my widget template:
<
telerik:RadListView
ID
=
"dynamicContentListView"
ItemPlaceholderID
=
"ItemsContainer"
runat
=
"server"
EnableEmbeddedSkins
=
"false"
EnableEmbeddedBaseStylesheet
=
"false"
>
<
LayoutTemplate
>
<
ul
class
=
"sfitemsList sfitemsListTitleDateTmb"
>
<
asp:PlaceHolder
ID
=
"ItemsContainer"
runat
=
"server"
/>
</
ul
>
</
LayoutTemplate
>
<
ItemTemplate
>
<
li
class
=
"sfitem sfClearfix"
>
<
h2
class
=
"sfitemTitle"
>
<
sf:DetailsViewHyperLink
ID
=
"DetailsViewHyperLink"
TextDataField
=
"Title"
runat
=
"server"
/>
</
h2
>
<
div
class
=
"itemTest"
>
<
strong
>
<
sf:SitefinityLabel
runat
=
"server"
Text
=
'Other Interests/Community Involvement:'
HideIfNoText
=
"true"
CssClass
=
"sfitemFieldLbl"
/>
</
strong
>
<
sf:SitefinityLabel
runat
=
"server"
Text='<%# Eval("TestField")%>' HideIfNoText="true" CssClass="sfitemRichText" />
</
div
>
<
sf:FieldListView
ID
=
"PublicationDate"
runat
=
"server"
Format
=
"PublicationDate.ToLocal():MMM d, yyyy, HH:mm tt"
WrapperTagName
=
"div"
WrapperTagCssClass
=
"sfitemPublicationDate"
/>
</
li
>
</
ItemTemplate
>
</
telerik:RadListView
>
The SitefinityLabel in green is the label in front of the value (it has class .sfitemFieldLbl, assigned to it). The yellow label is the value of my custom field (it has class .sfitemRichText, set to it). I wrapped both objects in a div with class .itemTest (in red). Here's the html markup I have:
<
div
class
=
"itemTest"
>
<
span
class
=
"sfitemFieldLbl"
>Other Interests/Community Involvement:</
span
>
<
span
class
=
"sfitemRichText"
>has value or is empty </
span
>
</
div
>
Here's the script I use to loop through all spans with class .sfitemRichText, check if their html is empty, and if so - remove the span, which is right above them (span with class .sfitemFieldLbl):
$(document).ready(function()
$("span.sfitemRichText").each(function()
if($.trim($(this).html()).length==0) $(this).prev('span').remove();
);
);
Hi Jen, thanks for the input.
Doing this client side would be easy yes, but it shouldn't be necessary and adds unnecessary weight to the rendered HTML. This should have been possible in the widget template editor, but alas it's not.
Regards,
Jacques
Hello Jacques,
I have tried using the following
<%# (Eval(
"Url"
) ==
null
|| Eval(
"Url"
).Equals(
string
.Empty)) ?
"EMPTY"
: Eval(
"Url"
) %>