6

如何使用事件处理程序覆盖组件的字段值?当我有下面的代码片段时,保存组件时没有错误。但是事件处理程序所做的内容更改不会反映在组件中。我希望单值字段“大小”具有“blabla ...”作为值。

// Call to Subscribe the events
EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveInitiatedHandler,
                                                EventPhases.Initiated); 

private void ComponentSaveInitiatedHandler(Component component, 
                                          SaveEventArgs args, EventPhases phases)
{
    if (component.Schema.Title == "XYZ")
    {
        ItemFields Fields = new ItemFields(component.Content, component.Schema);
        SingleLineTextField textField = (SingleLineTextField)Fields["size"];
        textField.Value = "blabla...";
    }
}
4

1 回答 1

9

您需要使用 XML 字符串更新 Content 属性,如下所示:

ItemFields Fields = new ItemFields(component.Content, component.Schema);
SingleLineTextField textField = (SingleLineTextField)Fields["size"];
textField.Value = "blabla...";
component.Content = Fields.ToXml();
于 2012-07-17T07:10:06.350 回答