我在使用OnCommand事件找到嵌套的Repeater时遇到问题。嵌套的Repeater(Rep3)在FooterTemplate中有一个LinkButton,有一些TextBoxes包含我需要发送给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");
}
答案 0 :(得分:0)
我在上面的标记中没有看到“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;
}