BaseDataBoundControl.DataSourceID Property
Gets or sets the ID of the control from which the data-bound control retrieves its list of data items.
Assembly: System.Web (in System.Web.dll)
Property Value
Type: System.StringThe ID of a control that represents the data source from which the data-bound control retrieves its data. The default is String.Empty.
If the data-bound control has already been initialized (the ConfirmInitState method is called or OnPagePreLoad event is handled) when you set the DataSourceID property, the OnDataPropertyChanged method is called, which sets the RequiresDataBinding property to true.
This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.
The following code example demonstrates how the DataSourceID property of a data-bound control is used. The GridView control is associated to the SqlDataSource control by setting its DataSourceID property to "AuthorsSqlDataSource", the ID of the SqlDataSource control. When the DataSourceID property is set (instead of the DataSource property), the data-bound control automatically binds to the data source control at run time.
<asp:sqldatasource id="CustomersSource" selectcommand="SELECT CustomerID, CompanyName, FirstName, LastName FROM SalesLT.Customer" connectionstring="<%$ ConnectionStrings:AWLTConnectionString %>" runat="server"/> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="False" emptydatatext="No data available." allowpaging="True" runat="server" DataKeyNames="CustomerID"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /> </Columns> </asp:gridview>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Most examples in the SDK demonstrate setting the DataSourceID to the ID property of the target DataSourceControl object (e.g., LinqDataSource or SqlDataSource). In most situations this is an acceptable and preferred approach. This, however, will fail if the DataSourceControl is in a different INamingContainer than the DataBoundControl (e.g., ListView). Common examples of naming containers include ContentPlaceHolder (i.e., a MASTER page template) and UserControl (i.e., an ASCX file).
For example, a page developer may wish to encapsulate the a popular complex template in a User Control, while still providing the flexibility of wiring it up to various data sources. Conversely, a site developer may place a frequently requested data source in the master page file, while still wanting to provide granular control over the presentation template.
In these scenarios, the fully qualified identifier (UniqueID) must be used, which follows the convention of concatenating each INamingContainer's ID property using the "$" symbol as a delimiter.
It's worth noting that it is not a best practice to hardcode the UniqueID in markup. This is because the UniqueID can change based on items outside the context of the markup. For example, if the DataBoundControl is inside a user control and the ID property of that control changes, then the UniqueID will change. For this reason, ideally the UniqueID should be relayed using a reference to the UniqueID property of the target control. For example, a user control containing a DataBoundControl might expose a DataSourceID property as a intermediary, which might be set as follows
<My:UserControl DataSourceID=<%# MyObjectDataSource.UniqueID %> RunAt="Server" />
This way, the value will always be correct, even if an ascendant NamingContainer changes its ID.
- 3/25/2011
- Jeremy Caney