簡體   English   中英

項目正在循環Repeater的ItemDataBound事件中的所有項目

[英]Items are looping all items in Repeater's ItemDataBound event

我的aspx頁面中有一個正常的Repeater Control

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

在按鈕上,單擊“我將數據綁定到Repeater

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

綁定后,將調用ItemDataBound事件。 在那我遍歷轉發器項進行一些操作

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

問題是循環每次都從頭開始。

對於Ex,如果我的數據為1 2 3,依此類推.....

它作為

1
1 2
1 2 3

我怎樣才能做到

1
2
3

我究竟做錯了什么

已經為中繼器中的每個項目調用了ItemDataBound ,因此您無需在那里進行循環。

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

旁注:適用於所有Data-Bound Web服務器控件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM