6

I am trying to use an if statement inside of a repeater control and receiving an InvalidOperationException on the if.

What I'm trying to do is run a block of code inside the repeater only if the current item has a UserType that is set to Admin.

     <asp:Repeater ID="rptSingleValueHeaders" runat="server">
         <ItemTemplate>
             <% if (Eval("UserType").ToString() == "Admin") { %>
                 <div>
                     do stuff here
                 </div>
            <% } else { %>
                 <div>
                     do other stuff
                 </div>
            <% } %>
         </ItemTemplate>
     </asp:Repeater>

My datasource is defined on the aspx.cs and contains a property named UserType which is of type string. Let me know if I need to provide any more details. Thank you.

noclist
  • 1,659
  • 2
  • 25
  • 66
  • possible duplicate of [If statement in repeaters ItemTemplate](http://stackoverflow.com/questions/17168851/if-statement-in-repeaters-itemtemplate) – emerson.marini Sep 12 '14 at 18:41
  • the stuff is displaying html markup – noclist Sep 12 '14 at 18:43
  • Answers in your referenced SO don't apply as I'm trying to evaluate a property coming from my datasource. – noclist Sep 12 '14 at 18:44
  • 1
    You question is a duplicate anyway. You can find the solution in the link. If not, there are some other questions that fit your requirements too. Just search and you'll find them. – emerson.marini Sep 12 '14 at 18:45
  • One from the ASP.NET forums: http://forums.asp.net/t/1574423.aspx?Forgot+how+to+use+if+statement+in+repeater+item+template – emerson.marini Sep 12 '14 at 18:47

1 Answers1

16

You could use server side visibility:

<ItemTemplate>
    <div runat="server" visible='<%# (Eval("UserType").ToString() == "Admin") %>'>
        I show this HTML
    </div>
    <div runat="server" visible='<%# (Eval("UserType").ToString() != "Admin") %>'>
        I show this other HTML
    </div>
</ItemTemplate>
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47