Control.TrackViewState Control.TrackViewState Control.TrackViewState Control.TrackViewState Method

定義

導致對伺服器控制項的檢視狀態變更的追蹤 (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 ()

範例

下列範例會覆寫DataBind自訂 ASP.NET 伺服器控制項中的方法。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 監視伺服器控制項的檢視狀態變更,這在您覆寫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.

適用於

另請參閱