我怎么能在我的ListView删除一个项目? [英] How I can remove a item in my ListView?

查看:152
本文介绍了我怎么能在我的ListView删除一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要不要删除我的asp.net的ListView一个条目,它不与我的code工作。

I want do delete a entry in my asp.net ListView and it don't work with my code.

我的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)
        {
            //
        }

我的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>

哪里错误?

推荐答案

侑可以使用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();
            }

        }

其中dataTable的是你绑定到ListView的数据源。

where dataTable is the datasource where you bind to your ListView.

这篇关于我怎么能在我的ListView删除一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆