ASP.NET - DataSourceID 和 DataSource 有什么区别?
DataSourceID和DataSource与控件属性有什么区别?我可以互换使用它们吗?
当我尝试在设计时设置 DataSource
属性时,通过在 aspx 页面上键入它,我收到一个异常,显示“无法以声明方式设置 'DataSource' 属性。”。但是当我使用属性窗口更改DataSource
时,它没有给出任何异常。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataSource
指实际的数据源对象,可以是.NET提供的数据源控件(如ObjectDataSource、SqlDataSource)或实际的数据对象,如DataTable、对象集合等。DataSourceID
是 .NET 提供的数据源控件的字符串标识符,此属性的存在使得数据绑定控件和相应的数据源可以在设计时在标记中关联起来。在内部,控件将使用提供的 ID 查找实际的数据源控件。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.DataSourceID
可以分配其他类型的数据源控件的 ID,例如ObjectDataSource
XmlDataSource
。数据源控件概述:
http://msdn.microsoft.com/en-us/ Library/ms227679.aspx
还有各种其他类型的数据源控件,或者您也可以开发自己的数据源控件。
当您想要直接提供控件源(例如
DataSet
或DataTable
)时,请使用Datasource
DataSourceID
can be assigned an ID of other data source control of type such asObjectDataSource
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 asDataSet
or aDataTable
数据源:< /a> 获取或设置数据绑定控件从中检索其数据项列表的对象。 (继承自
BaseDataBoundControl
。)DataSourceID: 获取或设置数据绑定控件从中检索其数据项列表的控件的 ID。 (继承自
DataBoundControl
。)其次,它们可以
互换
使用,但必须小心使用。例如,如果您先分配dataSourceID
,然后想使用DataSource
,那么在使用DataSource
之前,您必须通过设置清除datasourceID ControlID.DataSourceID = ""
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 assigndataSourceID
and then want to useDataSource
, then before usingDataSource
, you have to clear the datasourceID by settingControlID.DataSourceID = ""