4

Asp.net 4.5、C# を使用しています。私はいくつかの DataSource Bind を持つリープターを持っています:

  <asp:Repeater ItemType="Product" ID="ProductsArea" runat="server">
            <HeaderTemplate></HeaderTemplate>
            <ItemTemplate>
                ...  
            </ItemTemplate>
            <FooterTemplate></FooterTemplate>
        </asp:Repeater>    

このリピーター内で、現在の Iterated Item への参照が必要です。<%#Item%>を使用できることと、 を使用できることを知っています<%#Container.DataItem%>。フィールドに到達したい場合は、それを使用<%#Item.fieldName%>または評価できます。

しかし、フィールドに条件を付けたいのですが、次のようなことを行うために #Item への参照を取得するにはどうすればよいですか:

<% if (#Item.field>3)%>, <%if (#Container.DataItem.field<4)%> 

私は acautley がこのような参照を持ちたい <%var item = #Item%> 必要なときにいつでも使用できるようにしたいと考えています。

もちろん、上記の構文は無効です。このプロパティを達成するにはどうすればよいですか?

4

1 に答える 1

0

代わりに使用ItemDataBoundします。これにより、コードがはるかに読みやすく、保守しやすく、堅牢になります (コンパイル時の型の安全性)。

protected void Product_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        // presuming the source of the repeater is a DataTable:
        DataRowView rv = (DataRowView) e.Item.DataItem;
        string field4 = rv.Row.Field<string>(3); // presuming the type of it is string
        // ...
    }
}

e.Item.DataItem実際の型にキャストします。ItemTemplate使用中のコントロールを見つけe.Item.FindControlて適切にキャストする必要がある場合。もちろん、イベント ハンドラを追加する必要があります。

<asp:Repeater OnItemDataBound="Product_ItemDataBound" ItemType="Product" ID="ProductsArea" runat="server">
于 2014-12-10T12:42:05.710 に答える