I've created an asp:ListView
and attached it to an asp:XmlDataSource
. I would like to support the delete command for my list, so I've added the following button:
<asp:Button runat="server" CommandName="Delete"
Text="Del" CausesValidation="false" />
It throws an 'Specified method is not supported'. Any ideas on how to implement this delete?
An XmlDataSource
doesn't support the Delete
command directly, you have to manipulate your XML document manually. There's at least 2 ways you can handle this in your page.
CommandName
on your button to a command that doesn't exist by default, and do your XML editing in the ListView's ItemCommand
event handler. The event argument for the ItemCommand
event handler is of type ListViewCommandEventArgs
which has a CommandName
property which gets set to the value you set on your button.CommandName="Delete"
, and in the ItemDeleting
event you have to cancel the event (to keep it from trying to call Delete
on your XmlDataSource
control, which you've already seen doesn't work). Then do your XML editing here.As far as actually implementing the delete functionality, you'll have to manually edit the XML in your codebehind. From the XmlDataSource Class documentation on MSDN:
Updating XML Data
The XmlDataSource control is commonly used in read-only data scenarios where a data-bound control displays XML data. However, you can also use the XmlDataSource control to edit XML data. To edit the XML data, call the GetXmlDocument method to retrieve an XmlDataDocument object that is an in-memory representation of the XML data. You can use the object model exposed by the XmlDataDocument and XmlNode objects it contains or use an XPath filtering expression to manipulate data in the document. When you have made changes to the in-memory representation of the XML data, you can save it to disk by calling the Save method.
There are some restrictions to the editing capabilities of the XmlDataSource control:
- The XML data must be loaded from an XML file that is indicated by the DataFile property, not from inline XML specified in the Data property.
- No XSLT transformation can be specified in the Transform or TransformFile properties.
- The Save method does not handle concurrent save operations by different requests. If more than one user is editing an XML file through the XmlDataSource, there is no guarantee that all users are operating with the same data. It is also possible for a Save operation to fail due to these same concurrency issues.