views:

106

answers:

1

What is the best way to create a data bound ASP.NET user control?

I have a user control that contains a drop down list and three label controls. I would like to data bind the drop down list, and have the label controls display different properties of the data source.

I have Googled for an example of IDataSource and User Control, but cannot find any good information. This must be a common use of a user control?

A: 

The most straightforward approach is to add Bind(data) method to user control that delegates data to inner controls and do whatever you want. For example implementation may set DropDownList.DataSource property and then call DropDownList.DataBind... This approach may be used from any webpage processing stage (page_load, page_init, another control event handler...) and gives clear workflow to me. You always know when data is bound.

Another option is to override DataBind method and rely on standard asp.net webpage processing workflow.

Andrew Florko