EditorPart.SyncChanges 方法

定義

WebPart 控制項擷取屬性值,以供相關聯的 EditorPart 控制項使用。

public:
 abstract void SyncChanges();
public abstract void SyncChanges ();
abstract member SyncChanges : unit -> unit
Public MustOverride Sub SyncChanges ()

範例

下列程式代碼範例示範如何在自定義EditorPart控件中實SyncChanges作 方法。 如需執行範例所需的完整程式碼,請參閱類別概觀的 EditorPart 範例一節。

程式代碼範例的第一個部分示範在名為 TextDisplayEditorPart的自定義EditorPart類別中方法的SyncChanges實作。 這個方法會使用 WebPartToEdit 屬性取得相關聯TextDisplayWebPart控件的參考。 然後,它會取得 屬性的值TextDisplayWebPart.FontStyle,並更新下拉式清單控件中選取的專案, (自定義EditorPart控件上的屬性) 參考TextDisplayEditorPart.PartContentFontStyle

public override void SyncChanges()
{
  TextDisplayWebPart part = 
    (TextDisplayWebPart)WebPartToEdit;
  String currentStyle = part.FontStyle;

  // Select the current font style in the drop-down control.
  foreach (ListItem item in PartContentFontStyle.Items)
  {
    if (item.Value == currentStyle)
    {
      item.Selected = true;
      break;
    }
  }
}
Public Overrides Sub SyncChanges()
  Dim part As TextDisplayWebPart = CType(WebPartToEdit, _
                                         TextDisplayWebPart)
  Dim currentStyle As String = part.FontStyle

  ' Select the current font style in the drop-down control.
  Dim item As ListItem
  For Each item In PartContentFontStyle.Items
    If item.Value = currentStyle Then
      item.Selected = True
      Exit For
    End If
  Next item

End Sub

程序代碼範例的第二個部分示範相關聯的WebPart控件TextDisplayWebPart如何在此案例中建立相關聯的EditorPart控件集合 (,在方法的實CreateEditorParts作中,集合中只有一個名為 EditorPart 的控件 TextDisplayEditorPart) 。 當控件進入編輯模式時 TextDisplayWebPart ,就會執行這個方法。

public override EditorPartCollection CreateEditorParts()
{
  ArrayList editorArray = new ArrayList();
  TextDisplayEditorPart edPart = new TextDisplayEditorPart();
  edPart.ID = this.ID + "_editorPart1";
  editorArray.Add(edPart);
  EditorPartCollection editorParts = 
    new EditorPartCollection(editorArray);
  return editorParts;
}

public override object WebBrowsableObject
{
  get { return this; }
}
Public Overrides Function CreateEditorParts() _
                            As EditorPartCollection
  Dim editorArray As New ArrayList()
  Dim edPart as New TextDisplayEditorPart()
  edPart.ID = Me.ID & "_editorPart1"
  editorArray.Add(edPart)
  Dim editorParts As New EditorPartCollection(editorArray)
  Return editorParts

End Function

Public Overrides ReadOnly Property WebBrowsableObject() _
                                    As Object
  Get
    Return Me
  End Get
End Property

備註

方法 SyncChanges 是控件上 EditorPart 的重要方法。 它定義為類別中的 EditorPart 抽象方法,而且必須由繼承的控件實作。 方法的目的是要從 屬性中WebPartToEdit參考的WebPart控件擷取目前的值,並使用這些值更新控件中的EditorPart欄位,讓使用者可以編輯這些值。

SyncChanges每當關聯WebPart控件中的值可能已經變更時,就會呼叫 方法。 對於每個EditorPart控件,EditorZoneBase包含控件的區域會在呼叫 方法之後立即呼叫 SyncChangesApplyChanges 方法,讓控件中的值一律與相關聯WebPart控件中的EditorPart值同步處理。 另一WebPart個呼叫 方法的情況SyncChanges是當控件進入編輯模式時。

注意

SyncChanges如果該方法傳false回 ,則方法不會在 方法之後ApplyChanges呼叫,因為在此情況下發生錯誤。

給實施者的注意事項

衍生自 類別的 EditorPart 類別必須實作 SyncChanges() 方法。 實作的方法會使用 WebPartToEdit 屬性取得相關聯控件的參考,然後使用相關聯WebPart控件的屬性值來更新EditorPart控制件。

適用於

另請參閱