如何使用事件处理程序覆盖组件的字段值?当我有下面的代码片段时,保存组件时没有错误。但是事件处理程序完成的内容更改不会反映在Component中。我希望单值字段“size”具有“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...";
}
}
答案 0 :(得分:9)
您需要使用XML字符串更新Content属性,如下所示:
ItemFields Fields = new ItemFields(component.Content, component.Schema);
SingleLineTextField textField = (SingleLineTextField)Fields["size"];
textField.Value = "blabla...";
component.Content = Fields.ToXml();