Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I get data from listview in asp.net.I added button in my column. When I click button I want to delete selected row but not database. I researched internet but I dont find. What can I do?
Posted
Updated 17-May-18 19:51pm
Comments
ZurdoDev 5-Jan-16 9:40am    
You want to remove the row from the DataList on the page but not actually from the database? You might be able to set the row's Visible property.

XML
<asp:ListView ID="MyListView" runat="server" OnItemCommand="MyListView_ItemCommand">
    <ItemTemplate>
        <div>
            <asp:Literal ID="LiteralID" Text='<%#Eval("ID") %>' runat="server" />
            <asp:Button ID="ButtonRemove" CommandName="Remove" Text="Remove" runat="server" />
        </div>
    </ItemTemplate>
</asp:ListView>


in codebehind

C#
protected void MyListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Remove")
    {
        e.Item.Visible = false;
    }
}
 
Share this answer
 
Comments
ZurdoDev 5-Jan-16 11:51am    
+5
Use listView.Items.Remove(listView.SelectedItem) and you can call it from your delete button's click event.Or run a foreach loop and see if the item is selected, then remove it.
C#
foreach(var v in listView.SelectedItems)
{
   listView.Items.Remove(v)
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900