Control.TrackViewState 方法

定義

導致對伺服器控制項的檢視狀態變更的追蹤 (Tracking),以便它們能夠儲存於伺服器控制項的 StateBag 物件。Causes tracking of view-state changes to the server control so they can be stored in the server control's StateBag object. 這個物件可透過 ViewState 屬性存取。This object is accessible through the ViewState property.

protected:
 virtual void TrackViewState();
protected virtual void TrackViewState ();
abstract member TrackViewState : unit -> unit
override this.TrackViewState : unit -> unit
Protected Overridable Sub TrackViewState ()

範例

下列範例會覆寫自訂 ASP.NET 伺服器控制項中的 DataBind 方法。The following example overrides the DataBind method in a custom ASP.NET server control. 它一開始會呼叫基底 OnDataBinding 方法,然後使用 Clear 方法來刪除所有子控制項和 ClearChildViewState 方法,以刪除這些子控制項的任何已儲存的檢視狀態設定。It begins by calling the base OnDataBinding method and then uses the Clear method to delete all the child controls and the ClearChildViewState method to delete any saved view-state settings for those child controls. 最後,ChildControlsCreated 屬性會設定為 trueFinally, the ChildControlsCreated property is set to true. 控制項接著會使用 IsTrackingViewState 屬性來判斷控制項是否已啟用檢視狀態變更追蹤。The control then uses the IsTrackingViewState property to determine whether view-state change tracking is enabled for the control. 如果未啟用,則會呼叫 TrackViewState 方法。If it is not enabled, the TrackViewState method is called.

public override void DataBind() 
{
   base.OnDataBinding(EventArgs.Empty);
   // Reset the control's state.
   Controls.Clear();
   // Check for HasChildViewState to avoid unnecessary calls to ClearChildViewState.
   if (HasChildViewState)
      ClearChildViewState();
   ChildControlsCreated = true;
   if (!IsTrackingViewState)
      TrackViewState();
}
Public Overrides Sub DataBind()
   MyBase.OnDataBinding(EventArgs.Empty)
   ' Reset the control's state.
   Controls.Clear()
   ' Check for HasChildViewState to avoid unnecessary calls to ClearChildViewState.
   If HasChildViewState Then
      ClearChildViewState()
   End If
   ChildControlsCreated = True
   If Not IsTrackingViewState Then
      TrackViewState()
   End If
End Sub

備註

這個方法會在伺服器控制項生命週期中的 Init 事件結束時自動呼叫。This method is called automatically at the end of the Init event in the server control's lifecycle.

當您開發樣板化資料繫結控制項時,叫用這個方法。Invoke this method when you develop templated data-bound controls. 這個方法會警示 ASP.NET 來監視伺服器控制項之 view 狀態的變更,當您覆寫 Control.DataBind 方法時,這是必要的。This method alerts ASP.NET to monitor changes to a server control's view state, which is required when you override the Control.DataBind method.

適用於

另請參閱