locked
how to get selected checkbox index in repeater RRS feed

  • Question

  • User1717218719 posted

    I have the following code and am looking to find the index of the row in my repeater based on which checkbox is checked. my button is in a footer but the rows with the checkboxes are not in the footer. I am looking to delete the rows which have been checked. any help with this would be great.

                                <asp:Panel ID="pnlLbl" runat="server">
                                    <tr>
                                        <td>
                                            <asp:LinkButton ID="btnEdtItm" Text="Edit" runat="server" CommandName="Edit" CommandArgument='<%# Container.ItemIndex %>' />
                                            <asp:LinkButton ID="btnDelItm" Text="Delete" runat="server" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>' OnClientClick="return confirm('Do you want to delete this row?');" />
    
                                        </td>
                                        <td></td>
                                        <td>
                                            <asp:CheckBox ID="CheckBox1" runat="server" CommandName="CheckBox"/></td>
                                        <%--<asp:CheckBox ID="CheckBox2" runat="server" CommandName="CheckBox" CommandArgument='<%# Container.ItemIndex %>' /></td>--%>
                                    </tr>
    
    
                                </asp:Panel>
                            </ItemTemplate>
                            <FooterTemplate>
                                <tr>
                                    <td></td>
                                    <td>
                                        <asp:Label CssClass="Label" ID="TtlLbl" runat="server" Text="Total"></asp:Label></td>
                                   
                                </tr>
    
                                </table>
    
                                <asp:Button ID="Button2" Text="Delete Selected" runat="server" CommandName="DeleteSelected" CommandArgument='<%# Container.ItemIndex %>' OnClientClick="return confirm('Do you want to delete Selected row?');" />
                                <asp:Button ID="btnClear" Text="ClearAll" runat="server" CommandName="ClearAll" OnClientClick="return confirm('Do you want to delete All?');" />
    
    
                            </FooterTemplate>

                        If e.Item.ItemType = ListItemType.Footer Then
    
    
                            ctlTmp = CType(e.CommandSource, Button)
    
                            '-- Get the command from the control
                            Select Case ctlTmp.CommandName
    
                        '------CHECKBOX DELETE SELECTED 
                                Case "DeleteSelected"
    
                                    If ctlTmp.CommandName = "DeleteSelected" Then
    
                                        Dim intIdx = argcmndsrc.CommandArgument '--NOT CORRECT
                                        Dim itmRpt = Me.rptTaxCom.Items(intIdx)
    
    
                                        Dim dtbTaxComs As DataTable = GetTaxComItms()
    
                                        Dim cbox As CheckBox = TryCast(e.Item.FindControl("CheckBox1"), CheckBox)
    
                                        If cbox.Checked Then
    
                                            dtbTaxComs.Rows.RemoveAt(intIdx)
    
                                            '-- rebind after delete
                                            Me.rptTaxCom.DataSource = dtbTaxComs
                                            Me.rptTaxCom.DataBind()
    
                                        End If
                                    End If
                            End Select
                        End If

    Wednesday, June 26, 2019 8:37 AM

Answers

  • User288213138 posted

    Hi E.RU,

    You can get the index of the row which checkbox is checked by loop through the Repeater control .

    The code:

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As EventArgs)
        For i As Integer = 0 To Repeater1.Items.Count - 1
            Dim chb As CheckBox = TryCast(Me.Repeater1.Items(i).FindControl("CheckBox1"), CheckBox)
    
            If chb.Checked Then
                Response.Write(i)
            End If
        Next
    End Sub

    The result:

    But I tried, if you want to delete multiple rows at once, it is not possible to get the index of the current row by looping and then delete the selected row according to the index. Because your first loop ended, Repeater1.Items.Count also changed.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 26, 2019 11:05 AM