简体   繁体   English

如何在ListView中删除项目?

[英]How I can remove a item in my ListView?

I want do delete a entry in my asp.net ListView and it don't work with my code. 我想在asp.net ListView中删除一个条目,但它与我的代码不兼容。

my code: 我的代码:

protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

            if (e.CommandName == "Delete")
            {
                int index = Convert.ToInt32(e.CommandArgument);

                int listcount = ListView.Items.Count;

                if (listcount - 1 == index)
                {
                    ListView.Items.RemoveAt(index); //go do ListView_Deleting 
                }

            }

        }

        protected void ListView_SelectedIndexChanging(object sender, EventArgs e)
        {
            //
        }

        protected void ListView_Deleting(object sender, EventArgs e)
        {
            //
        }

my aspx: 我的aspx:

 <div class="InputControlBox">
            <asp:ListView ID="ListView" runat="server" OnItemCommand="ListView_ItemCommand"
            OnSelectedIndexChanging="ListView_SelectedIndexChanging" OnItemDeleting="ListView_Deleting">
                <LayoutTemplate>
                    ...
                </LayoutTemplate>
                <ItemTemplate>
                  ...
                </ItemTemplate>
            </asp:ListView>
        </div>

Where is the error? 错误在哪里?

yor can use the code 您可以使用代码

if (e.CommandName == "Delete")
        {
            int index = Convert.ToInt32(e.CommandArgument);

            int listcount = ListView.Items.Count;

            if (listcount - 1 == index)
            {
                dataTable.Rows[index].Delete();

                ListView.datasource = datatable;
                ListView.DataBind();
            }

        }

where dataTable is the datasource where you bind to your ListView. 其中dataTable是您绑定到ListView的数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM