Recommended way of adding conditional hyperlink to widget te

Posted by Community Admin on 04-Aug-2018 15:29

Recommended way of adding conditional hyperlink to widget template

All Replies

Posted by Community Admin on 23-Mar-2012 00:00

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>

I also imagine I could using inline conditions to check for null and then wrap the anchor inside that, but I am checking to see if this is already something built into an existing control. 


I did try the following, but it renders the entire link as text when I am looking to only display Map and have its href target the url.
<sitefinity:TextField runat="server" DisplayMode="Read" Value='<%# Eval("GoogleMapUrl")%>' WrapperTag="A" Title="Map" />

Posted by Community Admin on 28-Mar-2012 00:00

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>
where the Visible property will be set depending on whether there is a value for '<%# Eval("GoogleMapUrl")%>' or not.

Regards,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 10-Sep-2013 00:00

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>

== DBNull.Value doesn't  work, neither does == "". 

This is sitefinity 5.x

It renders out, but the conditions don't work so there's no template parsing issues. 

Posted by Community Admin on 10-Sep-2013 00:00

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" />

Posted by Community Admin on 11-Sep-2013 00:00

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 />

In the cases where there is an external link in my custom news field it correctly outputs 'something' but in cases where old news items wouldn't have a value it just outputs ''.

I'm stumped! It clearly picks up a difference in the values, but is not rendering what I would expect.

Any ideas?

Regards,
Jacques

Posted by Community Admin on 13-Sep-2013 00:00

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();
           );
       );

Hope this helps!

Regards,
Jen Peleva
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 13-Sep-2013 00:00

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

Posted by Community Admin on 18-Sep-2013 00:00

Hello Jacques,

I have tried using the following

<%# (Eval("Url") == null || Eval("Url").Equals(string.Empty)) ? "EMPTY" : Eval("Url") %>
in a project of mine where the Url property (string) belongs to a simple structure called Site.
I have a list of sites and the collection is bound to a list view.
This successfully resulted in Urls being returned for objects which property is not null or empty. And I also received 'EMPTY' for these two other cases.

So you can try to do that in your project no matter it is almost the same as previous suggestions and see if it works in your case. If not, we would probably need more code and knowledge about how and what is returned as a value for this ExternalLink in your custom news field. Simply said, we would probably need to debug your implementation on our side.

Please, let me know if this helps and if I can assist you further on the matter.

Regards,
Ivaylo Angelov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed