Export (0) Print
Expand All
2 out of 10 rated this helpful - Rate this topic

Control.DataBind Method

Binds a data source to the invoked server control and all its child controls.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public virtual void DataBind()

Use this method to bind a server control to data from a data source. This method is commonly used after retrieving a dataset through a database query. Most controls perform data binding automatically, which means that you typically do not need to call this method explicitly.

For more information, see How to: Create Templated ASP.NET User Controls and Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0. When called on a server control, this method resolves all data-binding expressions in the server control and in any of its child controls.

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 ControlCollection.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 and the control is instructed to track any changes to the view state of the newly created controls with the TrackViewState method. This is a common technique when binding data to a control to ensure that new data does not conflict with data stored from a previous DataBind method call.

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();
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.