在 OnCommand 事件中查找嵌套中继器
Find nested repeater on OnCommand event
我在使用 OnCommand 事件查找嵌套中继器时遇到问题。嵌套的 Repeater (Rep3) 在 FooterTemplate 中有一个 LinkButton,有一些带有我需要发送到 SQL 的数据的文本框。问题是我需要在 LinkButton 的 OcCommand 事件中引用包含 LinkButton 的 Repeater (Rep3)。下面是我的标记;
<asp:Repeater ID="Rep1" runat="server">
<ItemTemplate>
<asp:Repeater ID="Rep2" runat="server">
<ItemTemplate>
<table class="table table-striped table-condensed">
<asp:Repeater ID="Rep3" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButtonSave" runat="server" Text="Save" OnCommand="LinkButtonSave_OnCommand" />
</FooterTemplate>
</asp:Repeater>
</table>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
我试过如下找到中继器,但没有找到:
protected void LinkButtonSave_OnCommand(object sender, CommandEventArgs e)
{
LinkButton LinkButtonSave = (LinkButton) sender;
RepeaterItem riItem = (RepeaterItem) LinkButtonSave.NamingContainer;
Repeater Rep3 = (Repeater) riItem.FindControl("Rep3");
}
我在上面的标记中没有看到 "RepQuestions" 任何地方,但如果我理解正确的话,你正试图找到作为 LinkButtonSave 父级的转发器。以下是您的操作方法:
protected void Page_Load(object sender, EventArgs e)
{
Rep1.DataSource = new string[] { "Test" };
Rep1.DataBind();
}
protected void Rep1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
Repeater Rep2 = e.Item.FindControl("Rep2") as Repeater;
Rep2.ItemCreated += Rep2_ItemCreated;
Rep2.DataSource = new string[] { "Test" };
Rep2.DataBind();
}
protected void Rep2_ItemCreated(object sender, RepeaterItemEventArgs e)
{
Repeater Rep3 = e.Item.FindControl("Rep3") as Repeater;
Rep3.DataSource = new string[] { "Test" };
Rep3.DataBind();
}
protected void LinkButtonSave_OnCommand(object sender, CommandEventArgs e)
{
LinkButton LinkButtonSave = (LinkButton)sender;
// Here is the found repeater. The first parent returns the FooterTemplate
Repeater Rep3 = LinkButtonSave.Parent.Parent as Repeater;
}
我在使用 OnCommand 事件查找嵌套中继器时遇到问题。嵌套的 Repeater (Rep3) 在 FooterTemplate 中有一个 LinkButton,有一些带有我需要发送到 SQL 的数据的文本框。问题是我需要在 LinkButton 的 OcCommand 事件中引用包含 LinkButton 的 Repeater (Rep3)。下面是我的标记;
<asp:Repeater ID="Rep1" runat="server">
<ItemTemplate>
<asp:Repeater ID="Rep2" runat="server">
<ItemTemplate>
<table class="table table-striped table-condensed">
<asp:Repeater ID="Rep3" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButtonSave" runat="server" Text="Save" OnCommand="LinkButtonSave_OnCommand" />
</FooterTemplate>
</asp:Repeater>
</table>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
我试过如下找到中继器,但没有找到:
protected void LinkButtonSave_OnCommand(object sender, CommandEventArgs e)
{
LinkButton LinkButtonSave = (LinkButton) sender;
RepeaterItem riItem = (RepeaterItem) LinkButtonSave.NamingContainer;
Repeater Rep3 = (Repeater) riItem.FindControl("Rep3");
}
我在上面的标记中没有看到 "RepQuestions" 任何地方,但如果我理解正确的话,你正试图找到作为 LinkButtonSave 父级的转发器。以下是您的操作方法:
protected void Page_Load(object sender, EventArgs e)
{
Rep1.DataSource = new string[] { "Test" };
Rep1.DataBind();
}
protected void Rep1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
Repeater Rep2 = e.Item.FindControl("Rep2") as Repeater;
Rep2.ItemCreated += Rep2_ItemCreated;
Rep2.DataSource = new string[] { "Test" };
Rep2.DataBind();
}
protected void Rep2_ItemCreated(object sender, RepeaterItemEventArgs e)
{
Repeater Rep3 = e.Item.FindControl("Rep3") as Repeater;
Rep3.DataSource = new string[] { "Test" };
Rep3.DataBind();
}
protected void LinkButtonSave_OnCommand(object sender, CommandEventArgs e)
{
LinkButton LinkButtonSave = (LinkButton)sender;
// Here is the found repeater. The first parent returns the FooterTemplate
Repeater Rep3 = LinkButtonSave.Parent.Parent as Repeater;
}