Through CommandName='Delete' I try to delete a line from the ListView control but not from the datasource. On Pressing Delete I expect the webpage to reload and show me the updated ListView(with one line deleted). But nothing changes, the ListView will display the same content after pressing Delete. What do I do 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 behind:
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 don’t use the e.ExceptionHandled = true;, after pressing the Delete link the web page will come up with a ‘Specified method is not supported.’ message. Why?
If I use the above mentioned line, then the page refreshes but I can still see all original lines (although on debugging I can see that the ListVieItem collection now only contains an item less.)
It’s because of the DatasourceID parameter, which binds at every single postback on the original file.
What you should do is to bind your list on the first page load only. The delete button will work as you expect then.
— after comments.
OK. In fact, the Delete command would work if you had defined the Delete method on your datasource. Since that’s not what you want, you must define the ItemCommand event handler and tell it to remove the ListViewItem that issued the event.
It will do so without touching the XML file beneath. Do not databind against it, else the ‘deleted’ row will appear again.