Control.OnDataBinding(EventArgs) 方法

定義

引發 DataBinding 事件。

C#
protected virtual void OnDataBinding(EventArgs e);

參數

e
EventArgs

包含事件資料的 EventArgs 物件。

範例

下列範例示範如何覆寫 方法, OnDataBinding 以從數據源將子控件新增至父控件。

C#
// 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;
    }
}

備註

這個方法會通知伺服器控制項執行與它相關聯之系結數據的任何邏輯。

如果您想要處理 DataBinding 事件,您應該覆寫這個事件處理方法。 這可確保叫用附加至 DataBinding 事件的所有委派。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另請參閱