ASP.NET - What is the difference of DataSourceID and DataSource?

dpp picture dpp · Aug 17, 2011 · Viewed 20.3k times · Source

What is the difference of DataSourceID and DataSource from the controls' attribute? Can I use them interchangeably?

When I try to set DataSource property at the design time, by typing it on the aspx page, I get an exception that says "The 'DataSource' property cannot be set declaratively.". But when I use property window to change the DataSource, it gives no exception.

Answer

VinayC picture VinayC · Aug 17, 2011

DataSource refers to actual data source object which can be .NET provided data source controls (such as ObjectDataSource, SqlDataSource) or actual data objects such as DataTable, Collection of objects etc.

DataSourceID is the string identifier for .NET provided data source control and this property exists so that data-bound control and corresponding data source can be associated at the design time in markup. Internally, the control would look up for actual data source control using the id provided.