简体   繁体   中英

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.

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:

 <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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