+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Recommended way of adding conditional hyperlink to widget template

Recommended way of adding conditional hyperlink to widget template

8 posts, 0 answered
  1. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    23 Mar 2012
    Link to this post
    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" />
  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    08 Dec 2016
    28 Mar 2012
    Link to this post
    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
  3. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    10 Sep 2013 in reply to Boyan Barnev
    Link to this post
    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. 
  4. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    10 Sep 2013 in reply to Jacques
    Link to this post
    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" />
  5. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    11 Sep 2013 in reply to Stacey
    Link to this post
    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
  6. Jen Peleva
    Jen Peleva avatar
    568 posts
    Registered:
    10 Nov 2016
    13 Sep 2013
    Link to this post
    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
  7. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    13 Sep 2013 in reply to Jen Peleva
    Link to this post
    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
  8. Ivaylo Angelov
    Ivaylo Angelov avatar
    68 posts
    Registered:
    27 Oct 2016
    18 Sep 2013
    Link to this post
    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
8 posts, 0 answered