怎样可以点击button可以删除DataList内的一个数据

怎样可以点击button可以删除DataList内的一个数据

img

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ShoppingConnectionString %>" 
        SelectCommand="SELECT * FROM [CApple]" 
        DeleteCommand="DELETE FROM CApple WHERE [id] = @id">
        
        <DeleteParameters>
            <asp:QueryStringParameter Name="id" Type="String"/>
        DeleteParameters>
    asp:SqlDataSource>

    <asp:DataList ID="dlProduct" runat="server" DataSourceID="SqlDataSource1" 
        CellPadding="16" HorizontalAlign="Center" RepeatColumns="3" Width="620px" 
        DataKeyField="id">
        <ItemTemplate>
            <div class="divLeft">
            <img id="imgProduct" alt="" src='<%# Eval("img") %>' style="border-width:0px; width:250px;height:250px" runat="server" />
            div>
            <div  class="divLeft">
                <div class="productName">
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' Font-Size="15">asp:Label>
                div>
                <div class="productDescription">
                    <%# Eval("descn") %>
                div>
                <div class="itemText">
                    价格:<%# Eval("price") %>div>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>' ForeColor="White">asp:Label>
                
                <div class="itemText">
                    <asp:Button ID="Button1" runat="server" Text="取消收藏" 
                        onclick="Button1_Click" CommandArgument="'<%#Eval("id") %>'" CommandName="delete" />
                               
                    <asp:Button ID="Button2" runat="server" Text="加入购物车" />
                div>
            div> 
        ItemTemplate>
    asp:DataList>
        form>


你可以参考这个完整的简单示例:


简单来说就是通过 CommandName = “Delete” 来指定按钮为 DataList 控件的删除记录事件的触发器。
其中关键代码,请根据自己的数据库及其他细节进行相应修改:

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        con = new SqlConnection("<your connection string>");
        cmd.Connection = con;
        int RecordID = (int)DataList1.DataKeys[(int)e.Item.ItemIndex];
        cmd.CommandText = "Delete from yourTable where RecordID =" + RecordID ;
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        DataList1.EditItemIndex = -1;
        updateYourDataList();
    }

<asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete"> Delete </asp:LinkButton>

该回答引用GPTᴼᴾᴱᴺᴬᴵ
你可以在button的OnClick事件中,使用DataList控件的DeleteItem方法来删除DataList内的数据。你可以在Button1_Click方法中获取Button的CommandArgument属性值,然后将其转换为要删除的数据项的ID。然后使用DataList控件的DeleteItem方法,并将要删除的数据项的索引传递给该方法。
·
以下是实现的示例代码:

protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string id = btn.CommandArgument;
    int index = -1;

    for (int i = 0; i < dlProduct.Items.Count; i++)
    {
        Label lbl = (Label)dlProduct.Items[i].FindControl("Label2");
        if (lbl.Text == id)
        {
            index = i;
            break;
        }
    }

    if (index != -1)
    {
        dlProduct.DeleteItem(index);
    }
}


在Button1_Click事件处理程序中,首先获取被点击的button的CommandArgument属性值,即要删除的数据项的ID。然后,使用for循环在DataList控件的所有项中查找该ID,并获取该项的索引。最后,如果找到了要删除的项,则将其索引传递给DataList控件的DeleteItem方法。