2009-04-07 10 views
6

chciałbym zrobić coś takiego w ASP.NET 2.0:Testowanie Container.DataItem z kodem inline

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

Ale nie mogę przetestować DataBinder.Eval (Container.DataItem "myProperty"), jak to.

UWAGA: Nie mam dostępu do kodu źródłowego, mogę tylko zmienić ASPX w linii.

UWAGA2: Wiem, że mogę to wykorzystać:

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

ale szukałem czystszego sposób.

Czy istnieje sposób na przetestowanie Container.DataItem z wbudowanym kodem wewnątrz repeatera?

Odpowiedz

5

chciałbym to zrobić. Przywiązujesz swoją "widoczność" do widocznej właściwości asp: kontrola dosłowna:

<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> 
-4

Nie, nie ma innego sposobu, aby to zrobić.

2

Można go przekształcić w skrypt po stronie serwera.

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

i wezwanie go jako inline ...

<%#ShowIfEqual(Container, "SomeValue")%> 
1

Co z "innym przypadkiem"?

Wygląda brzydko, niezbyt czytelnie!

<asp:Repeater id="myRepeater" runat="server"> 
    <ItemTemplate> 
     <%--(if true part)--%> 
     <asp:literal runat='server' 
      visible='<%# DataBinder.Eval(Container.DataItem,"OrderCount") > 0%>'> 
      <%# DataBinder.Eval(Container.DataItem, "OrderCount") %> 
     </asp:literal> 
     <%--(else part)--%> 
     <asp:literal runat='server' 
      visible='<%# DataBinder.Eval(Container.DataItem,"OrderCount") ==0%>'> 
      <p>yet no orders</p> 
     </asp:literal> 
    </ItemTemplate> 
</asp:Repeater>