asp.netevalrepeaterdatabinder

checking value through IF in repeater


<asp:Repeater ID="rptEntries" runat="server" DataSourceID="dsEntries">
   <ItemTemplate>
      <%
         if (Convert.ToInt16(Eval("entryWinner")) == 1)
         {
      %>
            <%# "<div class=\"imgThumb draggable\" style=\"background-image:url('uploads/"
               + Eval("entryImagename") + "');\" data-id=\"" + Eval("pk_entryId")
               + "\"></div>"
            %>
      <%
          }else{
      %>
            <%# "<div class=\"imgThumb draggable\" style=\"background-image:url('uploads/"
                + Eval("entryImagename") + "'); filter: grayscale(100%);\" data-id=\""
                + Eval("pk_entryId") + "\"></div>"
            %>
      <%
           }
      %>
   </ItemTemplate>
</asp:Repeater>

I want to check te value from entryWinner before displaying, but it gives teh following error:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.


Solution

  • You may try something like that :

    <asp:Repeater ID="rptEntries" runat="server" DataSourceID="dsEntries">
       <ItemTemplate>
                <%# String.Format("<div class=\"imgThumb draggable\" style=\"background-image:url('uploads/{0}');{1}\"  data-id=\"{2}\"></div>"
    ,Eval("entryImagename")
    ,(Convert.ToInt16(Eval("entryWinner")) == 1)?"":" filter: grayscale(100%);",Eval("pk_entryId"))%>
           </ItemTemplate>
    
    </asp:Repeater>