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.NETControl.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.

適用於

另請參閱