asp.netvb.netnestedasprepeater

Get Checked checkboxes in nested repeater. Gives Error "Unable to cast object..."


I tried several example but every time I get an error on this line

 Dim rptChildMenu As repeater = DirectCast(ri.FindControl("childRepeater"), repeater)

Error: Unable to cast object of type 'System.Web.UI.WebControls.Repeater' to type 'repeater'. What am I missing here?

Here is my ASPX code.

<asp:Repeater ID="parentRepeater" runat="server">
    <ItemTemplate>
        <h6><i class="fa fa-fw fa-user"></i> <%# DataBinder.Eval(Container.DataItem, "cl_name") %></h6>
            <!-- start child repeater -->
            <asp:repeater id="childRepeater" datasource='<%# CType(Container.DataItem, System.Data.DataRowView).Row.GetChildRows("myrelation") %>' runat="server">

                <ItemTemplate>
                    <table>
                        <thead>
                            <tr>
                                <th style="width: 10%"></th>
                                <th style="width: 20px"></th>
                                <th style="width: 90%"></th>
                                <th style="width: 40px"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="chk" runat="server" CssClass="ChkBoxClass" Checked /></td>
                                <td>
                                    <label class="mb-2" for="chk<%# DataBinder.Eval(Container.DataItem, "[""id""]")%>"><%# DataBinder.Eval(Container.DataItem, "[""aantal""]")%></label>
                                    <asp:HiddenField ID="hiddenEmail" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "[""id""]")%>' />
                                </td>
                                <td>
                                    <a href="Art_Info.aspx?art_id=<%# DataBinder.Eval(Container.DataItem, "[""art_id""]")%>&tab_id=<%# DataBinder.Eval(Container.DataItem, "[""tab_id""]")%>"><i class="fas fa-fw"></i><%# DataBinder.Eval(Container.DataItem, "[""art_desc""]")%></a>
                                </td>
                                <td class="text-right align-middle">
                                    <div class="btn-group btn-group-sm text-left">
                                        <asp:LinkButton ID="LinkButton1" runat="server" Class="btn btn-danger" ><i class="fas fa-trash"></i></asp:LinkButton>
                                    </div>

                                </td>
                            </tr>
                        </tbody>
                    </table>
                </ItemTemplate>

            </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

<div class="mt-2 mb-3">
    <asp:Button ID="but_Send" runat="server" Text="Verzenden" class="btn btn-sm btn-primary float-left" />
</div>

Here my VB.NET code:

Inherits System.Web.UI.Page

'* * * This line comes from C# -> VB converter. But gives the error 'is already declared' * * *
'Protected parentRepeater As System.Web.UI.WebControls.Repeater

Private Sub repeater_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim conn As New MySqlConnection
    Dim sql As String

    conn.ConnectionString = ConnStr

    If Not IsPostBack Then


        sql = "SELECT cw_sales.cl_id, cl_name FROM cw_sales"
        sql += " INNER JOIN cw_clients ON cw_sales.cl_id = cw_clients.cl_id"
        sql += " WHERE og_id=" & 1
        sql += " GROUP BY cw_sales.cl_id;"

        Dim cmd1 As MySqlDataAdapter = New MySqlDataAdapter(Sql, conn)
        Dim ds As DataSet = New DataSet()
        cmd1.Fill(ds, "Clients")
        sql = "SELECT cw_sales.id, cw_sales.cl_id, cw_sales.art_id, art_desc, art_price1 AS art_price, tab_id, aantal FROM cw_sales"
        sql += " INNER JOIN article ON cw_sales.art_id = article.art_id"
        sql += " WHERE order_send=0;"

        Dim cmd2 As MySqlDataAdapter = New MySqlDataAdapter(sql, conn)
        cmd2.Fill(ds, "Orders")
        ds.Relations.Add("myrelation", ds.Tables("Clients").Columns("cl_id"), ds.Tables("Orders").Columns("cl_id"))
        parentRepeater.DataSource = ds.Tables("Clients")
        Page.DataBind()
        conn.Close()

    Else

    End If
End Sub


Public Sub but_Send_Click(sender As Object, e As EventArgs) Handles but_Send.Click
    For Each ri As RepeaterItem In parentRepeater.Items

        If ri.ItemType = ListItemType.Item OrElse ri.ItemType = ListItemType.AlternatingItem Then
            ' * * * Error showed on underlying line "Unable to cast object of type 'System.Web.UI.WebControls.Repeater' to type 'repeater'." * * *
            Dim rptChildMenu As repeater = DirectCast(ri.FindControl("childRepeater"), repeater)
            If rptChildMenu IsNot Nothing Then

                '* * * THis block works when there Is only ONE repeater * * * 
                Dim a As New ArrayList
                For Each i As RepeaterItem In rptChildMenu.Items
                    Dim cb As CheckBox = CType(i.FindControl("chk"), CheckBox)

                    If cb.Checked Then
                        Dim hiddenEmail As HiddenField = CType(i.FindControl("hiddenEmail"), HiddenField)
                        a.Add(hiddenEmail.Value)
                    End If
                Next
                '* * * End block
            End If
        End If
    Next
End Sub

You can download the mysql tables here http://bizarresoftware.be/repeater/KS_8_repeater.sql


Solution

  • Try this:

    Dim rptChildMenu As System.Web.UI.WebControls.Repeater = DirectCast(ri.FindControl("childRepeater"), System.Web.UI.WebControls.Repeater)