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.

注解

使用Control.DataBind(Boolean)方法在方案中自定义控件重写时DataBind方法,并实现IDataItemContainer接口。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.

备注

调用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.

另请参阅

适用于