1

我想删除我的 asp.net ListView 中的一个条目,但它不适用于我的代码。

我的代码:

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>

错误在哪里?

4

1 回答 1

6

你可以使用代码

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 的数据源。

于 2013-03-07T09:44:59.730 回答