C# 从 ListView 外部的控件中删除 ListView 中的项目

标签 c# asp.net listview

从 ListView 控件中,当选中复选框并从 ListView 外部单击按钮时,我需要能够删除已选中的项目。这是 ListView :

编辑

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack) return; 

    var notes = (from n in db.Notes
                 select n).OrderBy(n => n.FiscalYear).ThenBy(n => n.Period);

    notesListView.DataSource = notes;
    notesListView.DataBind();
}

<asp:ListView ID="notesListView" ItemPlaceholderID="itemPlaceholder" runat="server">
    <LayoutTemplate>
        <div>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
        </div>
    </LayoutTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="checkNote" runat="server" />
        <div><%# Eval("Period") %></div>
        <div><%# Eval("FiscalYear") %></div>
        <div><%# Eval("Account") %></div>
        <div class="wrap"><%# Eval("Description") %></div>
    </ItemTemplate>
    <EmptyDataTemplate>
        <div>No notes have been submitted.</div>
    </EmptyDataTemplate>
</asp:ListView>
<br />
<asp:Button ID="deleteNotes" CssClass="deleteButton" Text="Delete Notes" runat="server" OnClick="deleteNotes_Click" />

这是背后的代码:

protected void deleteNotes_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in notesListView.Items)
    {
        // I know this is wrong, just not sure what to do
        if (notesListView.SelectedIndex >= 0)
        {
            CheckBox ck = (CheckBox)item.FindControl("checkNote");
            if (ck.Checked)
            {
                    var index = item.DataItemIndex; // the item row selected by the checkbox
                    // db is the datacontext
                    db.Notes.DeleteOnSubmit(index);
                    db.SubmitChanges();
            }
        }
        else
        {
            errorMessage.Text = "* Error: Nothing was deleted.";
        }
    }
}

最佳答案

您需要在删除该对象之前找到该对象。为此,我将添加一个隐藏字段来存储 NoteID,如下所示:

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

在我的代码中,我通过 ID 查找注释并删除该注释(您可以使用任何其他唯一字段而不是 ID)。我的代码可能如下所示:

        ... ... ...
        int id = 0;
        var hdnId  = item.FindControl("hdnId") as HiddenField;
        CheckBox ck = (CheckBox)item.FindControl("checkNote");

        //I would check null for ck
        if (ck != null && ck.Checked && hdnId != null && int.TryParse(hdnId.Value, out id)
        {                    
                // db is the datacontext
                Note note = db.Notes.Single( c => c.Id== id );
                db.Notes.DeleteOnSubmit(note );
                db.SubmitChanges();
        }

关于C# 从 ListView 外部的控件中删除 ListView 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664973/

相关文章:

c# - VS2017 Code Review "These files have different encodings"我该如何解决这个问题?

c# - 表达式树在哪些情况下很有用?

asp.net - 加密 Web.Config 外部文件中的 AppSettings

c# - 资源管理器 7 上的大表性能不佳

c# - 在 C# 中读取并发送 mp4 文件

c++ - QComboBox ListView 正在占用组合框框架

c# - 用时区和夏令时确定一天的开始

android - 带有图像和文本以及可检查属性的 ListView

android - 与 getListAdapter Android 有并发症?

c# - 添加到我的 UserControl 的 TabControl 的 TabPage 的控件在重建后消失