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.

另請參閱

適用於