I am having a problem finding a nested Repeater using the OnCommand event. The nested Repeater (Rep3) has a LinkButton in the FooterTemplate, has some TextBoxes with data I need to send to SQL. The issue is that I need to reference the Repeater (Rep3) which contains the LinkButton inside the OcCommand event of the LinkButton. Below is my markup;
<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>
I tried find the repeater like below but with no luck:
protected void LinkButtonSave_OnCommand(object sender, CommandEventArgs e)
{
LinkButton LinkButtonSave = (LinkButton) sender;
RepeaterItem riItem = (RepeaterItem) LinkButtonSave.NamingContainer;
Repeater Rep3 = (Repeater) riItem.FindControl("Rep3");
}
I don't see "RepQuestions" any where in your mark-up above, but if I understand correctly you are trying to find the repeater which is the parent of LinkButtonSave. Here is how you do that:
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;
}