Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用复选框asp.net从ListView控件中删除所选行_Asp.net_Vb.net_Listview - Fatal编程技术网

使用复选框asp.net从ListView控件中删除所选行

使用复选框asp.net从ListView控件中删除所选行,asp.net,vb.net,listview,Asp.net,Vb.net,Listview,我正在我的网站上使用vb.net。我想知道我是否有Listview控件&我想通过复选框删除选中的行?我知道它可以通过使用“For Each”来实现,但如何执行我不知道。请帮帮我 <asp:ListView ID="productsList" runat="server" DataKeyNames=""> <LayoutTemplate>

我正在我的网站上使用vb.net。我想知道我是否有Listview控件&我想通过复选框删除选中的行?我知道它可以通过使用“For Each”来实现,但如何执行我不知道。请帮帮我

<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>

在ListView中,使用隐藏字段保存ID,如

<asp:HiddenField ID="hdnId" runat="server" Value='<%#Eval("ID") %>' />

希望这对你有帮助

列表中有复选框吗?请详细说明或发布屏幕截图/代码。是否要在检查时删除行?要从数据库中删除还是隐藏?要从数据库中删除database@Sami你能帮我做点什么吗?对不起,我还没有完全弄清楚你要做什么,因为你还有复选框和删除按钮。您想先选中复选框,然后单击“删除”按钮,执行删除过程,还是只选中复选框并删除该行?请澄清。谢谢你。虽然我想在vb.net中使用它。但现在我知道了逻辑部分,所以我将尝试在VBA中实现它是否解决了您的问题?我希望你这样做了:给我一些时间,因为我的问题得到解决,我会投票和接受的解决方案肯定..我只是在vb中转换了这个。我的删除按钮不在listview中,因此我必须使用onClick方法。。在我的帖子中,我提到了我的代码,如果选择了多行,则只会得到一行,因为delete查询的结构类似于delete FROM products,其中ID='123432654,'
<asp:HiddenField ID="hdnId" runat="server" Value='<%#Eval("ID") %>' />
 <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;                
        }

    }