I am trying to access datasource value of sublayout through code behind and I get empty string in a data source.
Below are the screen shots of my Banner sublayout in sitecore and how the datasource value is set in presentation:
Banner Sublayout with Datasource location and template set
Banner sublayout added to presentation and datasource set to an item
Below is the code that I am using to retrieve datasource:
using Sitecore.Data.Items;
using Sitecore.Web.UI.WebControls;
using System;
using System.Web.UI;
public partial class Banner : UserControl
{
private Item _dataSource = null;
public Item DataSource
{
get
{
if (_dataSource == null)
if (Parent is Sublayout)
{
_dataSource = Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
}
return _dataSource;
}
}
private void Page_Load(object sender, EventArgs e)
{
var test = DataSource;
Item a = Sitecore.Context.Item;
}
}
Am I doing anything wrong or what could be the issue?
What if you get your item renderings:
Then, get the rendering you want using:
And Finally,