0
votes

I have a simple aspx page which contains 2 nested repeaters. Each of these have buttons on them. When the user clicks on a button on the outer repeater, I can capture the ItemCommand in my codebehind, however when the user clicks on an inner one, I cannot capture it.

I have read in other threads about needing to attach the event manually to the inner repeater, but cannot work this out.

Can anyone help?

Here is the aspx. I'm using vb.net code behind

<asp:Repeater runat="server" ID="ParentRepeater">
   <ItemTemplate>
        <li id="P<%#DataBinder.Eval(Container, "DataItem.id")%>">
            <%#DataBinder.Eval(Container, "DataItem.name")%>
            <asp:Button runat="server" ID="adedit" Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>'
                class="pages-edit" />
            <asp:Button runat="server" ID="addel" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
                class="pages-delete" />
            <ul class="page-section sub innerdrag">
                <asp:Repeater runat="server" ID="childrepeater">
                    <ItemTemplate>
                        <li id="<%#DataBinder.Eval(Container, "DataItem.id")%>,">
                            <%#DataBinder.Eval(Container, "DataItem.name")%><asp:Button runat="server" ID="ad_edit"
                                Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>' class="pages-edit" />
                            <asp:Button runat="server" ID="ad_del" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
                                class="pages-delete" />
                        </li>
                    </ItemTemplate>
                </asp:Repeater>
            </ul>
        </li>
    </ItemTemplate>
</asp:Repeater>

Still not firing so I've put the updated VB here too

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim DBFunctions As New DBFunctions.Functions
            Dim dstmp As New DataSet
            Dim dstmp2 As New DataSet
            dstmp = DBFunctions.SQLDataSet("SELECT id,name, 'x'+cast(id as varchar(50)) as xid from pages where parent = 0 and coalesce(active,1)=1 order by orderby asc", "data")
            dstmp2 = DBFunctions.SQLDataSet("SELECT id,name , 'x'+cast(id as varchar(50)) as xid,parent from pages where parent >0 and coalesce(active,1)=1 order by orderby asc", "data2")
            Dim allData As New DataSet
            allData.Tables.Add(dstmp.Tables(0).Copy)
            allData.Tables.Add(dstmp2.Tables(0).Copy)
            allData.Relations.Add(New DataRelation("Children", allData.Tables(0).Columns("ID"), allData.Tables(1).Columns("parent")))
            ParentRepeater.DataSource = allData
            ParentRepeater.DataBind()
            sdhfunctions.Close()
        End If
    End Sub

    Protected Sub repMenu1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ParentRepeater.ItemDataBound
        Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
        If dv IsNot Nothing Then
            Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
            If repSubMenu IsNot Nothing Then
                AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
                repSubMenu.DataSource = dv.CreateChildView("Children")
                repSubMenu.DataBind()
            End If
        End If

    End Sub
    Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
        Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
        If dv IsNot Nothing Then
            Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
            If repSubMenu IsNot Nothing Then
                AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
                repSubMenu.DataSource = dv.CreateChildView("Children")
                repSubMenu.DataBind()
            End If
        End If

    End Sub

    Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
        Dim stophere As String = ""
    End Sub

I put a breakpoint on stophere, and it never gets hit.

Sorry guys, I'm really confused :(

1
Is this the real code you are using? There seems to be no subscription to the outer's Repeater ItemCommand event - Andrei
The itemcommend is on the buttons on the parent repeater, and I can capture thes in codebehind. However I cannot capture the itemcommand on the inner repeater buttons. - Lord Nodral III
Why would you ever want to capture this in code behind? What's wrong with just <asp:Repeater runat="server" ID="childrepeater" OnItemCommand="childrepeater_ItemCommand"> ? - Andrei
I need to capture a unique reference against the button which is clicked. Hence I put it in the itemcommand - Lord Nodral III
Very nice, but you already have this in place, and the event handler subscription is not supposed to have this information anyway. I still don't get it, to be honest - Andrei

1 Answers

0
votes

Try this:

Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
    Dim childRepeater As Repeater = DirectCast(e.Item.FindControl("childrepeater"), Repeater)
    AddHandler childRepeater.ItemCommand, AddressOf childrepeater_ItemCommand
    childRepeater.DataSource = "the data source for childRepeater"
    childRepeater.DataBind()
End Sub

Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)

End Sub