Export (0) Print
Expand All

Control.TrackViewState Method

Causes tracking of view-state changes to the server control so they can be stored in the server control's StateBag object. This object is accessible through the Control.ViewState property.

[Visual Basic]
Protected Overridable Sub TrackViewState()
[C#]
protected virtual void TrackViewState();
[C++]
protected: virtual void TrackViewState();
[JScript]
protected function TrackViewState();

Remarks

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. 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. For more information, see Developing a Templated Data-Bound Control.

Example

[Visual Basic, C#, C++] The following example overrides the DataBind method in a custom ASP.NET server control. 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. Finally, the ChildControlsCreated property is set to true. The control then uses the IsTrackingViewState property to determine whether view-state change tracking is enabled for the control. If it is not enabled, the TrackViewState method is called.

[Visual Basic] 
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

[C#] 
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();
}

[C++] 
public:
      void DataBind() 
      {
         Control::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();
      }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | IStateManager | ViewState | LoadViewState | SaveViewState | Developing a Templated Data-Bound Control

Show:
© 2014 Microsoft