项目正在循环 Repeater 的 ItemDataBound 事件中的所有项目
Items are looping all items in Repeater's ItemDataBound event
我的 aspx 页面中有一个正常的 Repeater
控件
<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
as
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
}
}
}
问题是循环每次都从头开始。
例如,如果我的数据是 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 Server Controls。
我的 aspx 页面中有一个正常的 Repeater
控件
<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
as
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
}
}
}
问题是循环每次都从头开始。
例如,如果我的数据是 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 Server Controls。