在OnCommand事件上查找嵌套转发器

时间:2015-08-15 13:04:28

标签: c# asp.net repeater

我在使用OnCommand事件找到嵌套的Repeater时遇到问题。嵌套的Repeater(Rep3)在FooterTemplate中有一个LinkBut​​ton,有一些TextBoxes包含我需要发送给SQL的数据。问题是我需要在LinkBut​​ton的OcCommand事件中引用包含LinkBut​​ton的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");
    }

1 个答案:

答案 0 :(得分:0)

我在上面的标记中没有看到“RepQuestions”,但如果我理解正确,你试图找到转发器,它是LinkBut​​tonSave的父级。这是你如何做到的:

    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;
    }