Control.DataBind 方法

定义

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

重载

DataBind()

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

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

将数据源绑定到调用的服务器控件及其所有子控件。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 ()

示例

下面的示例重写自定义 ASP.NET 服务器控件中的 DataBind 方法。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)

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

如果引发 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.

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

备注

在将 raiseOnDataBinding 设置为 false 时调用 Control.DataBind(Boolean) 方法将导致所有子控件都与 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.

另请参阅

适用于