Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a Container.DataItem with inline code

I would like to do something like this in ASP.Net 2.0:

 <asp:Repeater id="myRepeater" runat="server">
      <ItemTemplate>
           <% if (DataBinder.Eval(Container.DataItem, "MyProperty").Equals("SomeValue")) { %>
                <%#DataBinder.Eval(Container.DataItem, "MyProperty")%>
           <% } %>
      </ItemTemplate>
 </asp:Repeater>

But I cannot test the DataBinder.Eval(Container.DataItem, "MyProperty") like this.

NOTE: I don't have access to the source code, I can only change the aspx inline.

NOTE2: I know I can use this:

 <%#DataBinder.Eval(Container.DataItem, "MyProperty").Equals("SomeValue")?"<!--":""%>

but I was looking for a cleaner way.

Is there a way to test the Container.DataItem with inline code inside a Repeater?

like image 555
Filini Avatar asked Apr 07 '09 13:04

Filini


2 Answers

I would do this. You bind your "visibility" function to the visible property of an asp:literal control:

<asp:Repeater id="myRepeater" runat="server">
    <ItemTemplate>
        <asp:literal runat='server' id='mycontrol' 
          visible='<%# DataBinder.Eval(Container.DataItem, "MyProperty").Equals("SomeValue") %>'>
          <%# DataBinder.Eval(Container.DataItem, "MyProperty") %>
        </asp:literal>
     </ItemTemplate>
 </asp:Repeater>
like image 183
Keltex Avatar answered Oct 08 '22 19:10

Keltex


You could refactor it out to server side script.

<script runat="server">
protected string ShowIfEqual(RepeaterItem Item, string SomeValue) {
   YourTypeThatIsDataBound _item = (YourTypeThatIsDataBound)Item.DataItem;
   return _item.MyProperty == SomeValue ? _item.MyProperty : string.Empty;
}
</script>

And the call it inline as...

<%#ShowIfEqual(Container, "SomeValue")%>
like image 28
Fung Avatar answered Oct 08 '22 18:10

Fung