Control.TrackViewState Method


导致跟踪服务器控件的视图状态的更改,以便这些更改可以存储到服务器控件的 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.

 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() 
   // Reset the control's state.
   // Check for HasChildViewState to avoid unnecessary calls to ClearChildViewState.
   if (HasChildViewState)
   ChildControlsCreated = true;
   if (!IsTrackingViewState)
Public Overrides Sub DataBind()
   ' Reset the control's state.
   ' Check for HasChildViewState to avoid unnecessary calls to ClearChildViewState.
   If HasChildViewState Then
   End If
   ChildControlsCreated = True
   If Not IsTrackingViewState Then
   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.

Applies to

See also