This blog post contains information about how presentation components can use data source items in the Sitecore ASP.NET CMS.
You can specify a data source for each XSL rendering, web control, or sublayout. Presentation components typically read data from their data source item. XSL renderings apply the data source automatically, but code in web controls and sublayouts must account for the data source. The default data source for each presentation component is the context item, which is the item requested by the browser. For more information about rendering parameters and data sources, see the Presentation Component Reference.
Specifying a data source for a presentation component does not set the context item to that data source, or otherwise cause the presentation component to access its data source item. Each presentation component must explicitly access its data source.
The default boilerplate code for XSL renderings automatically sets the context element to the data source of the rendering. I think this may be why so many developers assume that Sitecore will apply the data source as the context item in .NET presentation components. Technically, the code defined by the first <xsl:template> element in each XSL rendering sets the context element to the data source item, and then invokes the code contained in the second <xsl:template> element. Code within the second <xsl:template> that accesses the context element implicitly accesses the data source of the rendering. Note the difference between the context item, which is the item requested by the browser, and the context element, which is the location of the XSL transformation engine in the source XML document. For more information about XSL with Sitecore, see the Presentation Component XSL Reference.
In web controls, the DataSource property in the base class Sitecore.Web.UI.WebControl indicates the data source as a string, such as by path or ID. You can use the GetItem() method also defined in this base class to access the data source of the rendering.
Sublayouts are standard ASP.NET web user controls that do not inherit from a Sitecore class. As such, sublayouts do not expose a property to specify the data source. Sitecore uses the Sitecore.Web.UI.WebControls.Sublayout web control to invoke sublayouts. Sitecore.Web.UI.WebControls.Sublayout inherits from Sitecore.Web.UI.WebControl, which exposes the DataSource property. A sublayout can access its data source through this property in at least three ways:
string ds = ((Sitecore.Web.UI.WebControls.Sublayout)this.Parent).DataSource;
If you absolutely need to access the data source of a web control or sublayout through the Sitecore.Context.Item property, you might be able to use the Sitecore.Data.Items.ContextItemSwitcher class. For example, you might not have the source code for an existing web control that accesses Sitecore.Context.Item. For the scope defined by the using statement that creates an instance, the Sitecore.Data.Items.ContextItemSwitcher temporarily changes the context item. For example, to set the context item during the rendering phase of a sublayout, override the Render() method to use a ContextItemSwitcher to invoke the Render() method in the base class with the data source as the context item.
protected override void Render(HtmlTextWriter writer)
Remember to use a Sitecore.Data.Items.ContextItemSwitcher in every method that needs to access the data source through Sitecore.Context.Item. For more information about .NET presentation components, see the Presentation Component API Cookbook.
To pass multiple data sources to a rendering, you can pass additional rendering parameters, or pass a data source containing a field that contains a multilist to specify those source items.
What's the difference between the web control approach and sublayouts? I thought a sublayout is just a wrapper around a control since each sublayout in Sitecore points to a ASCX file. I've been using the "quick and dirty" approach for a while but I wrapped it in a wrapper class that all of my controls inherit from, so I can always access DataSource right from the control.
@Mark: Web controls and user controls are two types of server controls, which are basically .NET classes that generally generate markup (though they could do other things, such as logging hits to a database). User controls are .ascx files with code-behind that inherits from System.Web.UI.UserControl, which inherits from System.Web.UI.Control. In Sitecore parlance, the term web control indicates a class that inherits from Sitecore.Web.UI.WebControl, which inherits from System.Web.UI.WebControl, which inherits from System.Web.UI.Control. So they're both types of controls, but web controls inherit from a Sitecore class, whereas user controls inherit from a system class. Sitecore uses the Sitecore.Web.UI.WebControls.Sublayout web control (which inherits from Sitecore.Web.UI.WebControl) to invoke sublayouts. Sitecore.Web.UI.WebControl supports features such as caching and data source. You can think of a web control as being like a web user control, but without the .ascx file - only the code. Web controls are appropriate for things that only generate markup dynamically, as opposed to setting properties of other controls nested within the .ascx file. You can use a web control for something that only generates markup, with no predefined markup. The Sitecore UI uses web controls almost exclusively without ever using user controls (.ascx files). It is completely legitimate to do everything with user controls and never use XSL or web controls. sdn.sitecore.net/.../Presentation Component Reference.aspx goes into more detail about each presentation component technology, and includes a section about the benefits and drawbacks of each. One valid summary of that section would be "do everything with sublayouts (user controls)."
This article might also be of interest: briancaos.wordpress.com/.../ In a nutshell, you can access the data source property by invoking: (Parent as Sitecore.Web.UI.WebControls.Sublayout).DataSource
More about data sources: learnsitecore.cmsuniverse.net/.../Simple-info-site-in-sitecore-part-7.aspx Datasource Location and Datasource Template: www.sitecore.net/.../Handling-Presentation-Component-Settings.aspx