如何删除使用asp:XmlDataSource的asp:ListView?

发布于 2024-10-29 04:35:38 字数 273 浏览 4 评论 0原文

我创建了一个 asp:ListView 并将其附加到 asp:XmlDataSource。我想支持列表的删除命令,因此我添加了以下按钮:

<asp:Button runat="server" CommandName="Delete" 
            Text="Del" CausesValidation="false" />

它会抛出“不支持指定的方法”。关于如何实现此删除有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-11-05 04:35:38

XmlDataSource 不直接支持Delete 命令,您必须手动操作 XML 文档。至少有两种方法可以在您的页面中处理此问题。

  • 将按钮上的 CommandName 更改为默认情况下不存在的命令,并在 ListView 的 ItemCommand 事件处理程序中进行 XML 编辑。 ItemCommand 事件处理程序的事件参数的类型为 ListViewCommandEventArgs,它具有一个 CommandName 属性,该属性设置为您在按钮上设置的值。
  • 将按钮保留为 CommandName="Delete",并且在 ItemDeleting 事件中,您必须取消该事件(以防止它尝试调用 Delete > 在您的 XmlDataSource 控件上,您已经看到它不起作用)。然后在此处进行 XML 编辑。

至于实际实现删除功能,您必须在代码隐藏中手动编辑 XML。 来自 MSDN 上的 XmlDataSource 类文档 :

更新 XML 数据

XmlDataSource 控件通常用于数据绑定控件显示 XML 数据的只读数据方案。但是,您还可以使用 XmlDataSource 控件来编辑 XML 数据。要编辑 XML 数据,请调用 GetXmlDocument 方法来检索 XmlDataDocument 对象,该对象是 XML 数据的内存中表示形式。您可以使用 XmlDataDocument 及其包含的 XmlNode 对象公开的对象模型,或使用 XPath 筛选表达式来操作文档中的数据。当您对 XML 数据的内存表示进行更改后,可以通过调用 Save 方法将其保存到磁盘。

XmlDataSource 控件的编辑功能有一些限制:

  • XML 数据必须从 DataFile 属性指示的 XML 文件加载,而不是从 Data 属性中指定的内联 XML 加载。
  • 不能在 Transform 或 TransformFile 属性中指定 XSLT 转换。
  • Save 方法不处理不同请求的并发保存操作。如果多个用户通过 XmlDataSource 编辑 XML 文件,则无法保证所有用户都使用相同的数据。由于这些相同的并发问题,保存操作也可能失败。

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.

  • Change the 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.
  • Leave the button with 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文