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 true用方法,並將設為。 Control.DataBind(Boolean) Control.DataBind()The Control.DataBind() method invokes the Control.DataBind(Boolean) method with raiseOnDataBinding set to true.

注意

呼叫方法並raiseOnDataBinding將設定為false ,會導致任何Control.DataBind()子控制項都與方法系結。 Control.DataBind(Boolean)Calling the Control.DataBind(Boolean) method with raiseOnDataBinding set to false causes any child controls to be data bound with the Control.DataBind() method.

另請參閱

適用於