navigator
navigator

Reputation: 1708

If statement inside Repeater control

Although there are several questions on this topic, I haven't found any satisfactory answers.

I have a Repeater that needs to display complex content. An IF statement is required within the template. I can't move this to the code-behind as I need to have server controls and user controls registered within the repeater. Here is what I need:

<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
    <ItemTemplate>
       <%# If CBool(Eval("IsFix")) Then%>
           <%-- HTML content including server and user controls --%>
       <%Else%>
           <%-- HTML content including server and user controls --%>
       <%End If%>
    </ItemTemplate>
</asp:Repeater>

The above throws a compiler error. Any idea on how to achieve this? I need to evaluate the IsFix field in the If statement.

Upvotes: 2

Views: 9986

Answers (4)

cjremond
cjremond

Reputation: 1

Remove # from the code where is having if statement.

Then use the code like this.

<%
                    object objIsFix = ((System.Data.DataTable)rep.DataSource).Rows[_nIndex]["IsFix"];
                if (bool(objIsFix)) {%>
                <%-- HTML content including server and user controls --%>
                <%}Else{%>
                <%-- HTML content including server and user controls --%>
                <%}%>

'_nIndex' is defined in the cs file

Upvotes: 0

Mauricio Mendoza
Mauricio Mendoza

Reputation: 11

It works perfectly!!

<%# If(CBool(Eval("bitExtemporaneo")) = True, "<img src=""Imagenes/Extempo.png"" alt=""Extemporaneo""/>", "")%>

Upvotes: 1

Jeremy
Jeremy

Reputation: 4838

I would put each set of content within a server side panel, then on ItemDataBound event, set the visibility of one panel to true, and the other to false. If visibility is set server side, then the front end content never even gets rendered.

Upvotes: 4

Rahul Singh
Rahul Singh

Reputation: 21795

Remove # from the code nuggets which is having if statement:-

<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
    <ItemTemplate>
       <% CBool(Eval("IsFix")) Then%>
           <%-- HTML content including server and user controls --%>
       <%Else%>
           <%-- HTML content including server and user controls --%>
       <%End If%>
    </ItemTemplate>
</asp:Repeater>

Edit:

Try with conditional operator (I am not sure about the syntax in VB.NET, please check):-

<%# If(CBool(Eval("IsFix")), "Do Something", "Else do something" %>

Upvotes: 2

Related Questions