Control.DataBind Method

Definition

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

Overloads

DataBind()

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

DataBind(Boolean)

Binds a data source to the invoked server control and all its child controls with an option to raise the DataBinding event.

DataBind()

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

public:
 virtual void DataBind();
public virtual void DataBind ();
abstract member DataBind : unit -> unit
override this.DataBind : unit -> unit
Public Overridable Sub DataBind ()

Examples

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

Remarks

Use this method to bind data from a source to a server control. 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.

This method is commonly overridden when you create a custom templated data-bound control. For more information, see How to: Create Templated ASP.NET User Controls and Developing Custom Data-Bound Web Server Controls. When called on a server control, this method resolves all data-binding expressions in the server control and in any of its child controls.

See also

Applies to

DataBind(Boolean)

Binds a data source to the invoked server control and all its child controls with an option to raise the DataBinding event.

protected:
 virtual void DataBind(bool raiseOnDataBinding);
protected virtual void DataBind (bool raiseOnDataBinding);
abstract member DataBind : bool -> unit
override this.DataBind : bool -> unit
Protected Overridable Sub DataBind (raiseOnDataBinding As Boolean)

Parameters

raiseOnDataBinding
Boolean

true if the DataBinding event is raised; otherwise, false.

Remarks

Use the Control.DataBind(Boolean) method in a scenario when your custom control overrides the DataBind method and implements the IDataItemContainer interface. In this scenario, the custom control calls the Control.DataBind(Boolean) method with raiseOnDataBinding set to false to ensure that the base class's DataBind method gets called.

The Control.DataBind() method invokes the Control.DataBind(Boolean) method with raiseOnDataBinding set to true.

Note

Calling the Control.DataBind(Boolean) method with raiseOnDataBinding set to false causes any child controls to be data bound with the Control.DataBind() method.

See also

Applies to