psdpainter
psdpainter

Reputation: 666

C# delete item from listview from control outside listview

From a listview control, when checkboxes are checked and a button is clicked from outside the listview, I need to be able to delete the items that have been checked. Here's the listview:

EDIT

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

Here's the code behind:

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.";
        }
    }
}

Upvotes: 2

Views: 1445

Answers (1)

afzalulh
afzalulh

Reputation: 7943

You need to find the object before deleting it. To do so, I would add a hiddenfield to store NoteID like this:

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

And in my code I am finding the note by ID and deleting that note (You may use any other unique field instead of ID). My code may look like this:

        ... ... ...
        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();
        }

Upvotes: 2

Related Questions