Control.OnDataBinding(EventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 DataBinding 事件。
protected:
virtual void OnDataBinding(EventArgs ^ e);
protected virtual void OnDataBinding (EventArgs e);
abstract member OnDataBinding : EventArgs -> unit
override this.OnDataBinding : EventArgs -> unit
Protected Overridable Sub OnDataBinding (e As EventArgs)
參數
範例
下列範例示範如何覆寫 方法, OnDataBinding 以從數據源將子控件新增至父控件。
// Override to create the repeated items from the DataSource.
protected override void OnDataBinding(EventArgs e) {
base.OnDataBinding(e);
if (DataSource != null) {
// Clear any existing child controls.
Controls.Clear();
// Clear any previous view state for the existing child controls.
ClearChildViewState();
// Iterate over the DataSource, creating a new item for each data item.
IEnumerator dataEnum = DataSource.GetEnumerator();
int i = 0;
while(dataEnum.MoveNext()) {
// Create an item.
RepeaterItem item = new RepeaterItem(i, dataEnum.Current);
// Initialize the item from the template.
ItemTemplate.InstantiateIn(item);
// Add the item to the ControlCollection.
Controls.Add(item);
i++;
}
// Prevent child controls from being created again.
ChildControlsCreated = true;
// Store the number of items created in view state for postback scenarios.
ViewState["NumItems"] = i;
}
}
' Override to create the repeated items from the DataSource.
Protected Overrides Sub OnDataBinding(E As EventArgs)
MyBase.OnDataBinding(e)
If Not DataSource Is Nothing
' Clear any existing child controls.
Controls.Clear()
' Clear any previous view state for the existing child controls.
ClearChildViewState()
' Iterate over the DataSource, creating a new item for each data item.
Dim DataEnum As IEnumerator = DataSource.GetEnumerator()
Dim I As Integer = 0
Do While (DataEnum.MoveNext())
' Create an item.
Dim Item As RepeaterItemVB = New RepeaterItemVB(I, DataEnum.Current)
' Initialize the item from the template.
ItemTemplate.InstantiateIn(Item)
' Add the item to the ControlCollection.
Controls.Add(Item)
I = I + 1
Loop
' Prevent child controls from being created again.
ChildControlsCreated = true
' Store the number of items created in view state for postback scenarios.
ViewState("NumItems") = I
End If
End Sub
備註
這個方法會通知伺服器控制項執行與它相關聯之系結數據的任何邏輯。
如果您想要處理 DataBinding 事件,您應該覆寫這個事件處理方法。 這可確保叫用附加至 DataBinding 事件的所有委派。