如何使用自定义DataSource从ListView中删除项目?

时间:2011-12-07 12:12:56

标签: c# asp.net

我有一个ListView,其中IEnumerable<MyDocument> DataSource来自某个方法。

myDocsList_ItemCommand()中的代码肯定会运行,因为文档实际上已删除。我的问题是ListView仍然显示(现在已删除)文档,直到下一页刷新,即使我有代码要执行myDocsList.Items.Remove(dataItem)

简化的.ascx基本上是:

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

简化的.ascx.cs基本上是:

protected void Page_Load(object sender, EventArgs e)
{
}

protected void Page_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        IEnumerable<MyDocument> 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);
    }
}

我一直在阅读ASP页面生命周期和一些相关的问题,但我对ASP有点缺乏经验并且有点迷失。

如何让我的ListView项目在PostBack上消失,而不是在下一页刷新时消失?

1 个答案:

答案 0 :(得分:6)

您必须将数据反弹到ListView ItemCommand事件中的//Create a new method for databind void BindData() { IEnumerable<MyDocument> docs = getDocuments(); myDocsList.DataSource = docs; myDocsList.DataBind(); } //Call databind method in your prerender event protected void Page_PreRender(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } //Again bind data after delete operation protected void myDocsList_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Delete") { int docId = int.Parse(e.CommandArgument.ToString()); deleteDocument(docId); BindData(); } } ,您可以更改您的编码风格,如下所示:

{{1}}