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

Forums / Developing with Sitefinity / If statement in widget template

If statement in widget template

6 posts, 0 answered
  1. ian
    ian avatar
    45 posts
    Registered:
    25 Feb 2013
    28 Mar 2014
    Link to this post

    I feel like i am close to getting this but i am getting a compilation error. I am using a widget template in sitefinity. I am trying to use a conditional statement based on three sets of radio buttons. Basically im trying to see if the text needs to be left right or center, dark or light and if the list item is the first to be shown. I think i am close with using the <%# eval %> but may be wrong by using the &&.

    here is an example of the code...

    <%# Eval("TextPosition") == 0 && Eval("ColorChoice") == 0 && Eval("IsFirst") == 0 ?
    <li ></li>
    %>

    any help would be much appreciated

  2. Ivan D. Dimitrov
    Ivan D. Dimitrov avatar
    310 posts
    Registered:
    26 Mar 2015
    31 Mar 2014
    Link to this post
    Hi Ian,

    In order to achieve this functionality, you need to use a sf:Conditional control placed in your ItemTemplate tag. Please view the sample below:
        <sf:Conditional If='<%# Eval("TestField") %>' runat="server">
        <Then>
            <div class="article_reclame">
        print something if yes
       </div>                                   
        </Then>
        <Else>
            <div>
         print something else if no
       </div>
        </Else>
    </sf:Conditional>
    The example evaluates a Yes/No custom field.

    Regards,
    Ivan D. Dimitrov
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Joel Kinzel
    Joel Kinzel avatar
    35 posts
    Registered:
    16 Jul 2009
    20 Apr 2015 in reply to Ivan D. Dimitrov
    Link to this post

    I'm just on a roll with digging up old posts, but this is almost working for me. The problem is if I have an eval statement inside the <if></if> or <then></then> blocks, it doesn't seem to work (SF 7.3). 

     Is this expected behavior? A bug? Or perhaps (most likely) a mistake on my part? My code is below and I get no HREF or TARGET on the links. If I add a runat="server" to them, nothing changes. 

     

    <Template>
        <sf:Conditional If='<%# Eval("HasChildNodes") %>' runat="server">
          <Then>
            <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" role="button" href='<%# NavigationUtilities.ResolveUrl(Container.DataItem) %>' target='<%# NavigationUtilities.GetLinkTarget(Container.DataItem) %>'><%# Eval("Title")%></a>
          </Then>
          <Else>
            <li>
            <a href='<%# NavigationUtilities.ResolveUrl(Container.DataItem) %>' target='<%# NavigationUtilities.GetLinkTarget(Container.DataItem) %>'><%# Eval("Title")%></a>
          </Else>
        </sf:Conditional>
        <ul class="dropdown-menu" runat="server" id="childNodesContainer"></ul>
        </li>
    </Template>
                       

  4. Elizabeth
    Elizabeth avatar
    83 posts
    Registered:
    23 Mar 2005
    11 Aug 2015
    Link to this post
    Can the sf:conditional control be used to evaluate if a field contains a string of text? If so, what is the syntax? Thanks.
  5. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    09 Dec 2016
    13 Aug 2015
    Link to this post
    Hi Elizabeth,

    I have just replied in the ticket you have opened for this. I am pasting the reply here as well for your convenience:

    What you can try is to cast the value of the custom text field to string and then check if the value is empty string (""). Here is an example:

    <sf:Conditional If='<%# Eval("CustomTextFieldName").ToString() != "" %>' runat="server">
        <Then>
            Here is the value of the custom field:
    <strong> <%# Eval("CustomTextFieldName") %> </strong>                            
        </Then>
        <Else>
            No value for the custom field
        </Else>
    </sf:Conditional>

    You can use the above as a sample and further modify it to fit your exact scenario.

    You may also refer to the following forum post where you can also see a sample usage of the <sf:Conditional> control with a custom text field.

    Regards,
    Sabrie Nedzhip
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  6. Dave
    Dave avatar
    4 posts
    Registered:
    10 Jun 2015
    20 Jul
    Link to this post

    I'm getting a peculiar result.  The code below renders the output in the "Then" clause correctly whenever the "locationURL" field has a value.  But it never renders the contents inside the "span" tag in the "Else" clause.  It will render the <span></span> but not the eval'ed contents inside.  Any ideas?

    <sf:Conditional If='<%# Eval("locationURL").ToString() != "" %>' runat="server">

        <Then>
            <a href='<%# Eval("locationURL") %>'> <%# Eval("Street")%> <%# Eval("City")%> <%# Eval("State")%> </a>
        </Then>
        <Else>
            <span> <%# Eval("Street")%> <%# Eval("City")%> <%# Eval("State")%> </span>
        </Else>
    </sf:Conditional>

6 posts, 0 answered