我在我的网站上使用vb.net。我想知道如果我有Listview Control&我想通过复选框删除所选行?我知道可以通过使用“For Each”来完成,但如何执行此操作我不知道。 pLease帮助我。
<asp:ListView ID="productsList" runat="server" DataKeyNames="">
<LayoutTemplate>
<table class="content-table products-table">
<thead>
<tr>
<td class="checkbox-part">
<asp:CheckBox runat="server" ID="chkAll" />
</td>
<td>
Image
</td>
<td class="product-title">
Name
</td>
<td>
Item Code
</td>
<td>
Budget
</td>
<td>
MOQ
</td>
<td>
Status
</td>
</tr>
</thead>
<div id="itemPlaceholderContainer" runat="server" style="">
<div runat="server" id="itemPlaceholder" />
</div>
</table>
</LayoutTemplate>
<ItemTemplate>
<tbody>
<tr>
<td class="checkbox-part">
<asp:CheckBox runat="server" ID="chk" />
</td>
<td>
<img src='<%#"../../" + Eval("image") %>' runat="server" class="lil-thumbnail" />
</td>
<td class="product-title">
<asp:Label ID="id" runat="server" Text='<%# Eval("ID") %>' Visible="false"></asp:Label>
<asp:Label ID="productName" runat="server" Text='<%# Eval("product_name") %>'></asp:Label><br />
<asp:hyperlink ID="Edit" runat="server" cssClass="editbtn icn" ToolTip="Edit Product" NavigateUrl='<%# "new-product.aspx?edit=edit" & "&" & "productID=" & Eval("ID") %>'></asp:hyperlink>
<asp:LinkButton ID="Delete" runat="server" CssClass="deletebtn icn" CommandName="DeleteProduct" CommandArgument='<%# Eval("ID")%>' ToolTip="delete" OnClientClick="return confirm('Are you sure you want to delete this Item?')" CausesValidation="false"></asp:LinkButton>
<asp:HyperLink ID="view" NavigateUrl="#" runat="server" CssClass="viewbtn icn" ToolTip="View Product"></asp:HyperLink>
</td>
<td>
<asp:Label ID="sku" runat="server" Text='<%# Eval("sku") %>'></asp:Label>
</td>
<td>
<asp:Label ID="price_range" runat="server" Text='<%# Eval("price_range") %>'></asp:Label>
</td>
<td>
<asp:Label ID="moq" runat="server" Text='<%# Eval("moq") %>'></asp:Label>
</td>
<td>
<asp:Label ID="status" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</td>
</tr>
</tbody>
</ItemTemplate>
</asp:ListView>
更新
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
答案 0 :(得分:0)
在ListView中使用隐藏字段来保存ID,如
<asp:HiddenField ID="hdnId" runat="server" Value='<%#Eval("ID") %>' />
添加OnItemCommand,如
<asp:ListView ID="productsList" runat="server" OnItemCommand="productsList_ItemCommand" >
在代码隐藏
中protected void productsList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string checkedItems = string.Empty;
if (e.CommandName == "DeleteProduct")
{
foreach (ListViewItem item in productsList.Items) {
CheckBox chk = (CheckBox)item.FindControl("chk");
if (chk == null) continue; //if no checkbox found, continue to foreach loop
if (chk.Checked) {
HiddenField hdnId = (HiddenField)item.FindControl("hdnId");
checkedItems += hdnId.Value + ",";
}
}
}
if (!string.IsNullOrEmpty(checkedItems))
{
//checkedItems has ID of checked items which you can use to delete from database
lbl.Text = checkedItems;
}
}
希望这能帮到你!