If statement in widget template

Posted by Community Admin on 04-Aug-2018 16:40

If statement in widget template

All Replies

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

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

Posted by Community Admin on 31-Mar-2014 00:00

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
 

Posted by Community Admin on 20-Apr-2015 00:00

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>
                   

Posted by Community Admin on 11-Aug-2015 00:00

Can the sf:conditional control be used to evaluate if a field contains a string of text? If so, what is the syntax? Thanks.

Posted by Community Admin on 13-Aug-2015 00:00

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
 

Posted by Community Admin on 20-Jul-2016 00:00

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>

This thread is closed