Asp.Net ListView how to delete a row without deleting from the data source

Via CommandName="Delete" I am trying to delete a row from a ListView control, but not from a data source. On Pressing Delete I expect the web page to reload and show me an updated ListView (with deleting one row). But nothing changes, the ListView will display the same content after clicking Delete. What am I doing wrong?

  <asp:ListView ID="ListView1" DataSourceID="XmlDataSource1" ItemContainerId="DataSection" runat="server"> <LayoutTemplate> <h3>Protocols to Upload...</h3> <table border=0 style="background-color:#9C9EFF; width: 100%;"> <tr align=left> <th>Region/Exam/Program</th><th>Protocol</th><th>Position</th> </tr> <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%#XPath("Location/Path")%></td> <td><%#XPath("Location/Name")%></td> <td><%#XPath("Location/Position")%></td> <td style="width:40px"> <asp:LinkButton ID="SelectCategoryButton" runat="server" Text="Select" CommandName="Select"/> </td> </tr> </ItemTemplate> <SelectedItemTemplate> <tr id="Tr1" runat="server" style="background-color:#F7F3FF"> <td><%#XPath("Location/Path")%></td> <td><%#XPath("Location/Name")%></td> <td><%#XPath("Location/Position")%></td> <td style="width:40px"> <asp:LinkButton runat="server" ID="SelectCategoryButton" Text="Delete" CommandName="Delete" /> </td> </tr> </SelectedItemTemplate> <%-- <ItemSeparatorTemplate> <div style="height: 0px;border-top:dashed 1px #ff0000"></div> </ItemSeparatorTemplate>--%> </asp:ListView> <asp:XmlDataSource ID="XmlDataSource1" XPath="HttpRequestBO/ProtocolsDTO/ProtocolDTO" runat="server" DataFile="~/HttpRequestBo.Sample.xml"></asp:XmlDataSource> 

And this is the code:

 protected void Page_Load(object sender, EventArgs e) { } protected void ListView1_OnItemDeleted(Object sender, ListViewDeletedEventArgs e) { if (e.Exception != null) { e.ExceptionHandled = true; } } protected void ListView1_OnItemCommand(object sender, ListViewCommandEventArgs e) { if (String.Equals(e.CommandName, "Delete")) { ListViewDataItem dataItem = (ListViewDataItem)e.Item; ListView1.Items.Remove(dataItem); } } 

If I do not use e.ExceptionHandled = true; , after clicking the "Delete" link on the web page, "The specified method is not supported." message. Why?

If I use the above line, the page refreshes, but I still see all the source lines (although when debugging, I see that the ListVieItem collection now contains only the less item.)

+4
source share
1 answer

This is because of the DatasourceID parameter, which is bound every time the source file is accessed.

What you need to do is link your list only to the loading of the first page. The delete button will work as you expect.

--- after the comments.

OK In fact, the Delete command will work if you define the Delete method on your data source. Since this is not what you want, you must define an ItemCommand event handler and tell it to remove the ListViewItem that issued the event.

 protected void yourListView_OnItemCommand(object sender, ListViewCommandEventArgs e) { if (String.Equals(e.CommandName, "Delete")) { ListViewDataItem dataItem = (ListViewDataItem)e.Item; yourListView.Items.Remove(dataItem); } } 

He will do this without touching the XML file below. Do not bind the data binding to it, otherwise the line β€œdeleted” will reappear.

+3
source

All Articles