Search code examples
c#asp.netrepeaterasprepeater

Conditional loading in repeater


in my repeater i have to load 2 out of 4 different images depends on some conditions.

here is 4 images

if (<%# DataBinder.Eval(Container.DataItem, "Voted") %> == "true") 'load these 2 images
    <img style="cursor: pointer" onclick="Dostuff"
         id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
         src="../../Images/badge-circle-plus-24-ns.png" />
    <img style="cursor: pointer" onclick="Dostuff"
         id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
         src="../../Images/arrow-down-24-ns.png" />
else 'load these 2 
    <img style="cursor: pointer" onclick="Dostuff" 
         id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
         src="../../Images/arrow-up-24-ns.png"  />
    <img style="cursor: pointer" onclick="Dostuff"
         id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
         src="../../Images/arrow-down-24-ns.png"  />

How do i get around this problem?


Solution

  • You can; see this for more information. Essentially, you should format it as:

    <% if (DataBinder.Eval(Container.DataItem, "Voted")  == "true") {  %>
        <img style="cursor: pointer" onclick="Dostuff"
             id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
             src="../../Images/badge-circle-plus-24-ns.png" />
        <img style="cursor: pointer" onclick="Dostuff"
             id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
             src="../../Images/arrow-down-24-ns.png" />
    <% } else { %>
        <img style="cursor: pointer" onclick="Dostuff" 
             id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
             src="../../Images/arrow-up-24-ns.png"  />
        <img style="cursor: pointer" onclick="Dostuff"
             id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
             src="../../Images/arrow-down-24-ns.png"  />
    <% } %>
    

    I'm not sure if that's completely supported, so you could also consider trying:

    <img style="cursor: pointer" onclick="Dostuff"
             id='<%# DataBinder.Eval(Container.DataItem, "Uid") %>'
             src='../../Images/<%# DataBinder.Eval(Container.DataItem, "Voted")  == "true" ? "badge-circle-plus-24-ns.png" : "arrow-up-24-ns.png" %>' />
      .
      .
    

    The latter uses a conditional and renders the appropriate image depending on the condition.