如何在子 Repeater 的 ItemCommand 事件中获取当前 Repeater
How to get current Repeater in ItemCommand event of child Repeater
这是HTML代码:
<asp:Repeater ID="Repeater1" runat="server"
OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<h1>Repeater 1</h1>
<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
<ItemTemplate>
<h1>Repeater 2</h1>
<asp:LinkButton CommandArgument='<%#Container.ItemIndex%>' CommandName="cmdDeleteItem" ID="lnkDelete" runat="server" >Delete</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
这是隐藏代码:
protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "cmdDeleteItem":
{
var Repeater2= (Repeater)e.Item.FindControl("Repeater2");
var list = new ArrayList(Repeater2.Items);
list.Add(Repeater2.Items.Count);
Repeater2.DataSource = list;
Repeater2.DataBind();
}
break;
}
}
Repeater2_ItemCommand事件可能是找不到自己的Repeater(Repeater2)。
您不能使用 FindControl
方法。在这种情况下,您应该像这样将源参数转换为 Repeater
:
var Repeater2 = (Repeater)source;
然后您可以访问触发 item_command
事件的转发器的所有属性。
这是HTML代码:
<asp:Repeater ID="Repeater1" runat="server"
OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<h1>Repeater 1</h1>
<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
<ItemTemplate>
<h1>Repeater 2</h1>
<asp:LinkButton CommandArgument='<%#Container.ItemIndex%>' CommandName="cmdDeleteItem" ID="lnkDelete" runat="server" >Delete</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
这是隐藏代码:
protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "cmdDeleteItem":
{
var Repeater2= (Repeater)e.Item.FindControl("Repeater2");
var list = new ArrayList(Repeater2.Items);
list.Add(Repeater2.Items.Count);
Repeater2.DataSource = list;
Repeater2.DataBind();
}
break;
}
}
Repeater2_ItemCommand事件可能是找不到自己的Repeater(Repeater2)。
您不能使用 FindControl
方法。在这种情况下,您应该像这样将源参数转换为 Repeater
:
var Repeater2 = (Repeater)source;
然后您可以访问触发 item_command
事件的转发器的所有属性。