Control.TrackViewState Control.TrackViewState Control.TrackViewState 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.

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.

适用于

另请参阅