防止在 umbraco 中删除具有子节点的内容

发布于 2024-08-31 22:10:53 字数 548 浏览 0 评论 0原文

我想防止内容节点在有子节点时被丢弃。我像这样设置了一个事件处理程序:

public class KeepSafeEvents : ApplicationBase
{
    public KeepSafeEvents()
    {
        Document.BeforeMoveToTrash += new Document.MoveToTrashEventHandler(Document_BeforeMoveToTrash);
    }

    void Document_BeforeMoveToTrash(Document sender, umbraco.cms.businesslogic.MoveToTrashEventArgs e)
    {
        if (sender.HasChildren)
        {
            e.Cancel = true;
        }
    }
}

但是,这似乎不起作用。我认为这是因为删除过程在处理父节点(然后没有子节点)之前首先将子节点移动到垃圾箱。还有其他可能的解决方案吗?或者我在上面犯了一个简单的错误?

I would like to prevent content nodes from being trashed if they have any children. I setup an event handler like so:

public class KeepSafeEvents : ApplicationBase
{
    public KeepSafeEvents()
    {
        Document.BeforeMoveToTrash += new Document.MoveToTrashEventHandler(Document_BeforeMoveToTrash);
    }

    void Document_BeforeMoveToTrash(Document sender, umbraco.cms.businesslogic.MoveToTrashEventArgs e)
    {
        if (sender.HasChildren)
        {
            e.Cancel = true;
        }
    }
}

However, this doesn't seem to work. I assume it is because the delete process moves the child nodes to trash first before dealing with the parent node (which then has no children). Is there another possible solution? Or am I making a simple mistake above?

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

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

发布评论

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

评论(1

绝不放开 2024-09-07 22:10:53

这段代码非常适合我。您确定已将生成的 .dll 文件复制到 Umbraco 的 /bin 文件夹中吗?

我只是写得比你短一点,如下所示,但功能应该完全相同。

我确实注意到带有子节点的文档似乎被删除(它从树中消失),但是当您重新加载树时,该节点仍然存在。

public class KeepSafeEvents : ApplicationBase
{
  public KeepSafeEvents()
  {
    Document.BeforeMoveToTrash += Document_BeforeMoveToTrash;
  }

  void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e)
  {
    if (sender.HasChildren)
      e.Cancel = true;
  }
}

This code works perfectly for me. Are you sure that you've copied the resulting .dll file to Umbraco's /bin folder?

I just wrote it a little shorter than you did, like below, but the functionality should be exactly the same.

I do notice that the document with childnode seems to get deleted (it disappears from the tree), but when you reload the tree, the node is still there.

public class KeepSafeEvents : ApplicationBase
{
  public KeepSafeEvents()
  {
    Document.BeforeMoveToTrash += Document_BeforeMoveToTrash;
  }

  void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e)
  {
    if (sender.HasChildren)
      e.Cancel = true;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文