If statement in widget template - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

If statement in widget template

  • If statement in widget template
  • 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

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

  • Can the sf:conditional control be used to evaluate if a field contains a string of text? If so, what is the syntax? Thanks.
  • 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
     
  • 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>