Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the DataBinding event fire during the ASP .NET page lifecycle?

Simply, if I have a GridView with a SqlDataSource control declarative set as its data source, when does that data source retrieve its data and when does the binding take place in the page lifecycle?

like image 237
Pete Michaud Avatar asked Aug 07 '09 14:08

Pete Michaud


2 Answers

In the 'preRender' phase - look at the ASP.NET Page Life Cycle Overview for more info.

like image 77
n8wrl Avatar answered Oct 04 '22 21:10

n8wrl


This article about Page Cycle is pretty good.

For example, suppose you have a GridView that displays a company record in each row along with a list of the company officers in a ListBox control. To fill the list of officers, you would bind the ListBox control to a data source control (such as SqlDataSource) that retrieves the company officer data using the CompanyID in a query.

If the ListBox control's data-binding properties, such as DataSourceID and DataMember, are set declaratively, the ListBox control will try to bind to its data source during the containing row's DataBinding event. However, the CompanyID field of the row does not contain a value until the GridView control's RowDataBound event occurs. In this case, the child control (the ListBox control) is bound before the containing control (the GridView control) is bound, so their data-binding stages are out of sync.

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

like image 41
Max Avatar answered Oct 04 '22 23:10

Max