Control.OnDataBinding Method
.NET Framework 2.0
Raises the DataBinding event.
Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
The following example demonstrates how to override the OnDataBinding method to add child controls to the parent control from a data source.
// 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 void OnDataBinding(EventArgs e)
{
super.OnDataBinding(e);
if (get_DataSource() != null) {
// Clear any existing child controls.
get_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 = get_DataSource().GetEnumerator();
int i = 0;
while (dataEnum.MoveNext()) {
// Create an item.
RepeaterItem item = new RepeaterItem(i, dataEnum.get_Current());
// Initialize the item from the template.
get_ItemTemplate().InstantiateIn(item);
// Add the item to the ControlCollection.
get_Controls().Add(item);
i++;
}
// Prevent child controls from being created again.
set_ChildControlsCreated(true);
// Store the number of items created in view state for postback
// scenarios.
get_ViewState().set_Item("NumItems", (Int32)i);
}
} //OnDataBinding