Asp.net Relay - get Refrence to current element

I am using Asp.net 4.5, C #. I have an adapter that has a DataSource binding to it:

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

      

Within this repeater, I would like to mention the current iterated item. I know what I can use <%#Item%>

and what I can use <%#Container.DataItem%>

. If I want to get into the field, I can use <%#Item.fieldName%>

either Eval it.

But I want to create a condition for the field. How can I get the refrence for #Item to do something like this:

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

      

I would like acautley to have a refrain like this <%var item = #Item%

> and than use it when I need to.

Of course the above syntax is not valid. How do you achieve this?

+3


source to share


1 answer


I would use instead ItemDataBound

. This makes the code much more readable, convenient, and reliable (compile time type safety).

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
        // ...
    }
}

      



Enter e.Item.DataItem

into actual type. If you need to find a control in ItemTemplate

, use e.Item.FindControl

and render it accordingly. Of course, you must add an event handler:

<asp:Repeater OnItemDataBound="Product_ItemDataBound" ItemType="Product" ID="ProductsArea" runat="server">

      

0


source







All Articles