15 August, 2011

Data Binding Events for Data-Bound Controls in Asp.net

it is very important to know when particular event will fire in data-bound control.

To help you understand the relationship between the page life cycle and data binding events, the following table lists data-related events in data-bound controls such as the GridView, DetailsView, and FormView controls.


Control Event Typical Use
DataBinding
  • Raised after the control's PreRender event, which occurs after the page's PreRender event. (This applies to controls whose DataSourceID property is set declaratively. Otherwise the event happens when you call the control's DataBind method.)
  • This event marks the beginning of the process that binds the control to the data. Use this event to manually open database connections, if required, and to set parameter values dynamically before a query is run.
RowCreated (GridView only) or ItemCreated (DataList, DetailsView, SiteMapPath, DataGrid, FormView, Repeater, and ListView controls)
  • Raised after the control's DataBinding event.
  • Use this event to manipulate content that is not dependent on data binding. For example, at run time, you might programmatically add formatting to a header or footer row in a GridView control.
RowDataBound (GridView only) or ItemDataBound (DataList, SiteMapPath, DataGrid, Repeater, and ListView controls)
  • Raised after the control's RowCreated or ItemCreated event.
  • When this event occurs, data is available in the row or item, so you can format data or set the FilterExpression property on child data source controls in order to display related data within the row or item.
DataBound
  • Raised at the end of data-binding operations in a data-bound control. In a GridView control, data binding is complete for all rows and any child controls.
  • Use this event to format data-bound content or to initiate data binding in other controls that depend on values from the current control's content.


Nested Data-Bound Controls

  1. put the data source control for the Child control in the same template item as the Child control itself, and do not set the data binding properties of the Child control declaratively. 
  2. Instead, set them programmatically at run time during the RowDataBound event, so that the Child control does not bind to its data until the ParentId information is available.

No comments:

Post a Comment