简体   繁体   中英

Items are looping all items in Repeater's ItemDataBound event

I have a normal Repeater Control in my aspx page

 <asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="chks" runat="server" />
<asp:TextBox ID="txtName" runat="server" CssClass="form-control" Text='<%# DataBinder.Eval(Container,"DataItem.Name").ToString()%>'></asp:TextBox><asp:Label ID="lblValue" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container,"DataItem.Id").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>

On the button click I'm binding data to the Repeater as

rpt1.DataSource = GetData();
rpt1.DataBind();

After binding the ItemDataBound event is called. In that I'm looping through the repeater items for some manipulations

protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    foreach (RepeaterItem item in rpt1.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
             string val = ((Label)item.FindControl("lblValue")).Text;
            // Some Stuff
        }
    }
}

The problem is that the loop is getting started from first every time.

For Ex if my data is as 1 2 3 so on.....

It is iterarting as

1
1 2
1 2 3

How can I make it as

1
2
3

What am I doing wrong

ItemDataBound is already called for every item in the repeater, so you don't need a loop there.

protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         string val = ((Label)e.Item.FindControl("lblValue")).Text;
        // Some Stuff
    }
}

Side-Note: that applies to all Data-Bound Web Server Controls .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM