如何在我的ListView中删除一个项目
本文关键字:一个 项目 删除 我的 ListView | 更新日期: 2023-09-27 18:02:37
我想在我的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>
错误在哪里?
您可以使用代码
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();
}
}