Delete Selected Rows from ListView Control using Checkbox asp.net
<p>I am using vb.net in my website. I want to know If I have Listview Control &amp; I want to delete selected rows by checkbox? I know it can be done by using 'For Each but how to execute this I have no idea. pLease help me.</p>

<pre><code>&lt;asp:ListView ID="productsList" runat="server" DataKeyNames=""&gt;
                                        &lt;LayoutTemplate&gt;
                                            &lt;table class="content-table products-table"&gt;
                                                &lt;thead&gt;
                                        &lt;tr&gt;
                                            &lt;td class="checkbox-part"&gt;
                                                &lt;asp:CheckBox runat="server" ID="chkAll" /&gt;
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                Image
                                            &lt;/td&gt;

                                            &lt;td class="product-title"&gt;
                                                Name
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                               Item Code
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                Budget
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                MOQ
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                Status
                                            &lt;/td&gt;
                                        &lt;/tr&gt;
                                    &lt;/thead&gt;
                                                &lt;div id="itemPlaceholderContainer" runat="server" style=""&gt;
                                                    &lt;div runat="server" id="itemPlaceholder" /&gt;
                                                &lt;/div&gt;
                                            &lt;/table&gt;
                                        &lt;/LayoutTemplate&gt;

                                        &lt;ItemTemplate&gt;
                                            &lt;tbody&gt;
                                        &lt;tr&gt;
                                            &lt;td class="checkbox-part"&gt;
                                                &lt;asp:CheckBox runat="server" ID="chk" /&gt;
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                &lt;img src='&lt;%#"../../" + Eval("image") %&gt;' runat="server" class="lil-thumbnail" /&gt;
                                            &lt;/td&gt;

                                            &lt;td class="product-title"&gt;
                                                &lt;asp:Label ID="id" runat="server" Text='&lt;%# Eval("ID") %&gt;' Visible="false"&gt;&lt;/asp:Label&gt;
                                                &lt;asp:Label ID="productName" runat="server" Text='&lt;%# Eval("product_name") %&gt;'&gt;&lt;/asp:Label&gt;&lt;br /&gt;
                                                &lt;asp:hyperlink ID="Edit" runat="server" cssClass="editbtn icn" ToolTip="Edit Product" NavigateUrl='&lt;%# "new-product.aspx?edit=edit" &amp; "&amp;" &amp; "productID=" &amp; Eval("ID") %&gt;'&gt;&lt;/asp:hyperlink&gt;

                                                &lt;asp:LinkButton ID="Delete" runat="server" CssClass="deletebtn icn" CommandName="DeleteProduct" CommandArgument='&lt;%# Eval("ID")%&gt;' ToolTip="delete" OnClientClick="return confirm('Are you sure you want to delete this Item?')" CausesValidation="false"&gt;&lt;/asp:LinkButton&gt;

                                                &lt;asp:HyperLink ID="view" NavigateUrl="#" runat="server" CssClass="viewbtn icn" ToolTip="View Product"&gt;&lt;/asp:HyperLink&gt;
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                &lt;asp:Label ID="sku" runat="server" Text='&lt;%# Eval("sku") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                &lt;asp:Label ID="price_range" runat="server" Text='&lt;%# Eval("price_range") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                &lt;asp:Label ID="moq" runat="server" Text='&lt;%# Eval("moq") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/td&gt;

                                            &lt;td&gt;
                                                &lt;asp:Label ID="status" runat="server" Text='&lt;%# Eval("status") %&gt;'&gt;&lt;/asp:Label&gt;
                                            &lt;/td&gt;
                                        &lt;/tr&gt;
                                    &lt;/tbody&gt;
                                        &lt;/ItemTemplate&gt;
                                    &lt;/asp:ListView&gt;
</code></pre>

<p>UPDATE</p>

<pre><code>Protected Sub deleteMultiple()
        Dim checkedItems As String = String.Empty
        For Each item As ListViewItem In productsList.Items
            Dim chk As CheckBox = DirectCast(item.FindControl("chk"), CheckBox)
            If chk Is Nothing Then
                Continue For
            End If
            'if no checkbox found, continue to foreach loop
            If chk.Checked Then
                Dim hdnId As HiddenField = DirectCast(item.FindControl("hdnId"), HiddenField)
                checkedItems += hdnId.Value + ","
            End If
        Next

        If Not String.IsNullOrEmpty(checkedItems) Then
            'checkedItems has ID of checked items which you can use to delete from database
            Dim lbl As String
            lbl = checkedItems
            Try
                Dim con As New MySqlConnection
                Dim query As New MySqlCommand
                con.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString()
                query.Connection = con
                con.Open()
                query.CommandText = "DELETE FROM products WHERE ID = @ID"
                query.Parameters.AddWithValue("@ID", lbl)
                query.ExecuteNonQuery()
                con.Close()
                Response.Redirect(Request.RawUrl)
            Catch ex As Exception
                Response.Write(ex)
            End Try
        End If
    End Sub
</code></pre>