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

梦想与她 提交于 2019-12-01 14:07:24

问题


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.


回答1:


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.




回答2:


DataSourceID can be assigned an ID of other data source control of type such as ObjectDataSource XmlDataSource .

Data Source Controls overview:

http://msdn.microsoft.com/en-us/library/ms227679.aspx

There are various other types of data source controls as well or you can develop your own as well.

Datasource is used when you want to directly give the source to control such as DataSet or a DataTable




回答3:


DataSource: Gets or sets the object from which the data-bound control retrieves its list of data items. (Inherited from BaseDataBoundControl.)

DataSourceID: Gets or sets the ID of the control from which the data-bound control retrieves its list of data items. (Inherited from DataBoundControl.)

Secondly they can be used interchangeably, but you have to use carefully. e.g. If you first assign dataSourceID and then want to use DataSource, then before using DataSource, you have to clear the datasourceID by setting ControlID.DataSourceID = ""



来源:https://stackoverflow.com/questions/7088017/asp-net-what-is-the-difference-of-datasourceid-and-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!