Control.DataBind Control.DataBind Control.DataBind Control.DataBind Method

定义

将数据源绑定到被调用的服务器控件及其所有子控件。Binds a data source to the invoked server control and all its child controls.

重载

DataBind() DataBind() DataBind() DataBind()

将数据源绑定到被调用的服务器控件及其所有子控件。Binds a data source to the invoked server control and all its child controls.

DataBind(Boolean) DataBind(Boolean) DataBind(Boolean) DataBind(Boolean)

将数据源绑定到调用的服务器控件及其所有子控件,同时可以选择引发 DataBinding 事件。Binds a data source to the invoked server control and all its child controls with an option to raise the DataBinding event.

DataBind() DataBind() DataBind() 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 ()

示例

下面的示例重写DataBind自定义 ASP.NET 服务器控件中的方法。The following example overrides the DataBind method in a custom ASP.NET server control. 首先调用基OnDataBinding方法,然后ControlCollection.Clear使用方法删除所有子控件,并ClearChildViewState使用方法删除这些子控件的任何已保存的视图状态设置。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. 最后, ChildControlsCreated将属性设置为true ,并指示控件通过TrackViewState方法跟踪对新创建的控件的视图状态所做的任何更改。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. 这是将数据绑定到控件以确保新数据不会与以前DataBind的方法调用存储的数据发生冲突的常用技术。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

注解

使用此方法可将数据从源绑定到服务器控件。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. 有关详细信息,请参阅如何:创建模板化 ASP.NET 用户控件和开发自定义数据绑定 Web 服务器控件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.

另请参阅

DataBind(Boolean) DataBind(Boolean) DataBind(Boolean) DataBind(Boolean)

将数据源绑定到调用的服务器控件及其所有子控件,同时可以选择引发 DataBinding 事件。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)

参数

raiseOnDataBinding
Boolean Boolean Boolean Boolean

如果引发 DataBinding 事件,则为 true;否则为 falsetrue if the DataBinding event is raised; otherwise, false.

注解

当自定义控件DataBind重写方法并实现IDataItemContainer接口时,请使用方法。Control.DataBind(Boolean)Use the Control.DataBind(Boolean) method in a scenario when your custom control overrides the DataBind method and implements the IDataItemContainer interface. 在此方案中,自定义控件调用Control.DataBind(Boolean)方法, raiseOnDataBinding并将false设置为,以确保调用基类DataBind的方法。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.

方法调用方法,并raiseOnDataBinding将设置为trueControl.DataBind(Boolean) Control.DataBind()The Control.DataBind() method invokes the Control.DataBind(Boolean) method with raiseOnDataBinding set to true.

备注

如果调用Control.DataBind(Boolean)raiseOnDataBinding方法设置为false ,则会使任何Control.DataBind()子控件都与方法进行数据绑定。Calling the Control.DataBind(Boolean) method with raiseOnDataBinding set to false causes any child controls to be data bound with the Control.DataBind() method.

另请参阅

适用于