How do I delete an item from a ListView using a custom DataSource?
<p>I have a <code>ListView</code> with an <code>IEnumerable&lt;MyDocument&gt;</code> <code>DataSource</code> pulled from a method.</p>

<p>The code in <code>myDocsList_ItemCommand()</code> definitely runs, because the document is actually deleted. My problem is that the <code>ListView</code> still shows the (now deleted) document until the next page refresh, even though I have code to do <code>myDocsList.Items.Remove(dataItem)</code>.</p>

<p>The simplified .ascx is basically:</p>

<pre><code>&lt;asp:ListView id="myDocsList" runat="server" 
        OnItemDataBound="myDocsList_ItemDataBound" 
        OnItemDeleting="myDocsList_ItemDeleting" 
        OnItemCommand="myDocsList_ItemCommand"&gt;
    &lt;LayoutTemplate&gt;
        &lt;table&gt;
            &lt;asp:Placeholder id="itemPlaceholder" runat="server" /&gt;
        &lt;/table&gt;
    &lt;/LayoutTemplate&gt;
    &lt;ItemTemplate&gt;
        &lt;tr&gt;&lt;td&gt;
                &lt;asp:LinkButton 
                    ID="delete" runat="server"
                    CommandName="Delete" CommandArgument="X"
                    OnClientClick="javascript:return confirm('...');"&gt;
                        Delete
                &lt;/asp:LinkButton&gt;
        &lt;/td&gt;&lt;/tr&gt;
    &lt;/ItemTemplate&gt;
&lt;/asp:ListView&gt;
</code></pre>

<p>The simplified .ascx.cs is basically:</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
}

protected void Page_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        IEnumerable&lt;MyDocument&gt; docs = getDocuments();
        myDocsList.DataSource = docs;
        myDocsList.DataBind();
    }
}

/* so we have the ID of the document we're deleting later on */
protected void myDocsList_ItemDataBound(object sender, 
                                        ListViewItemEventArgs e)
{
    var deleteButton = 
        (LinkButton) ((Control) e.Item).FindControl("delete");
    deleteButton.CommandArgument =
        ((MyDocument) e.Item.DataItem).id.ToString();
}

/* or we get "raised event ItemDeleting which wasn't handled" */
protected void myDocsList_ItemDeleting(Object sender, 
                                        ListViewDeleteEventArgs e)
{
}

/* do something here? */
//protected void myDocsList_ItemDeleted(Object sender, 
//                                      ListViewDeletedEventArgs e)
//{
//}

protected void myDocsList_ItemCommand(object sender, 
                                        ListViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int docId = int.Parse(e.CommandArgument.ToString());
        deleteDocument(docId);
        ListViewDataItem dataItem = (ListViewDataItem) e.Item;
        myDocsList.Items.Remove(dataItem);
    }
}
</code></pre>

<p>I've been reading up on the ASP page lifecycle and a few related questions, but I'm reasonably inexperienced with ASP and a bit lost.</p>

<p>How do I get my ListView items to disappear on the PostBack instead of on the next page refresh?</p>
