Can I change a value of a property in a TreeNode in a DocumentEvents.Update.Before event?

◇◆丶佛笑我妖孽 提交于 2020-01-02 23:16:58

问题


A specific field in the TreeNodes we are using needs to be unique among its siblings. To ensure that it is unique, I'm trying to change the values of a field in a TreeNode when the user changes and saves it. This causes the save event to fire again, which calls my event handler, which fires the event again. It is an undesired loop.

Is there a way to update the treenode, without firing the update event?

The only way I could think of is performing an sql query directly to the database, but I hope there's a better way.

TreeNode.SubmitChanges(false); and TreeNode.Update(); both fire the DocumentEvents.Update event, so that did not solve the problem.

I don't see any differences between the DocumentEventArgs when the event is fired from the Kentico User Interface or when it is fired from my C# code.

Event handler call:

DocumentEvents.Update.Before += 
uniquePageAliasModule.EnsureUniquePageAliasOnUpdate;

Event handler implementation:

treeNode.SetValue((Metadata.MetadataPageUrlPath), path);
treeNode.SubmitChanges(false);

Desired: Save the changes to the TreeNode and do not fire the DocumentEvents.Update event

Actual: Saves the changes to the TreeNode and fires the DocumentEvents.Update event


回答1:


You can use RequestStockHelper or Recursion Control like it was mentioned in this article. I personally prefer to use RequestStockHelper it works like request context.

You might try DocumentEvents.Update.Continue = false. there is an old topic on that




回答2:


You can temporarily suppress the DocumentEvents.Update event.

DocumentEvents.Update.Allow = false;
treeNode.Update();
DocumentEvents.Update.Allow = true;

Or as a method:

public void Save(BaseInfo baseInfo, bool doFireSaveEvent)
{
    var allowsUpdateEvents = DocumentEvents.Update.Allow;

    DocumentEvents.Update.Allow = doFireSaveEvent;
    try
    {
        baseInfo.Update();
    }
    catch (Exception e)
    {
        EventLogProvider.LogEvent(
            "E",
            $"{nameof(TreeNodeRepository)}.{ nameof(TreeNodeRepository.Save)}",
            "TREENODE_CAN_NOT_BE_SAVED",
            e.Message);
    }
    finally
    {
        DocumentEvents.Update.Allow = allowsUpdateEvents;
    }
}



回答3:


Try something like this:

TreeNode treeNode = e.Node;
treeNode.SetValue((Metadata.MetadataPageUrlPath), path);
treeNode.Update();

Kentico should prevent event loop on it's own.



来源:https://stackoverflow.com/questions/54399387/can-i-change-a-value-of-a-property-in-a-treenode-in-a-documentevents-update-befo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!