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.

另請參閱

適用於