我正在尝试获取代码背后的子布局项(或项id)。,这有可能吗?
更新:
我指的不是Datasource项或当前项,而是Sublayout呈现定义项。这与子布局文件有1到1的关系.
代码隐藏文件:
public partial class Product_Sublayout : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Sublayout subLayout = this.Parent as Sublayout;
Item subLayoutItem = null; //How to get sublayout rendering definition item here?
}
}发布于 2011-05-11 12:40:34
此页将解释细节,但下面是您需要的代码:
Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);更新:
您可能可以通过使用数据库来通过ID获取呈现项本身:
Sitecore.Context.Database.GetItem(((Sublayout)Parent).RenderingID);发布于 2011-08-26 13:57:54
不需要循环遍历控件或根据控件名称检查子布局。
完成任务的正确方法是遵循以下步骤:
以下是实现你的目标的守则:
LayoutDefinition layoutDef = LayoutDefinition.Parse(Sitecore.Context.Item.Fields["__renderings"].Value);
string deviceId = Sitecore.Context.Device.ID.ToString();
DeviceDefinition curDeviceDef = layoutDef.GetDevice(deviceId);
RenderingDefinition renderingDef = curDeviceDef.GetRendering(Sitecore.Context.Database.Items["/sitecore/Layout/SubLayouts/MySublayout"].ID.ToString());
int controlIndex = curDeviceDef.GetIndex(renderingDef.UniqueId);
Control MyDotNetControl = Sitecore.Context.Page.Renderings[controlIndex].GetControl();现在,您有了对点网控件的引用。只需将其类型强制转换到相应的控件,以获得对对象的完全访问。
发布于 2011-05-11 12:17:20
要获取用于将特定控件放置到页面中的项,可以使用一个名为的共享源模块。模块可以找到这里
如果要检索该项,可以考虑使用以下设置:
道具:
public partial class Afbeeldingen : System.Web.UI.UserControl
{
/// <summary>
/// Datasource item of the current rendering
/// </summary>
private Sitecore.Data.Items.Item dataSource = null;
/// <summary>
/// Helper object to get the rendering params, datasource and rendering
/// </summary>
private SublayoutParamHelper paramHelper = null;
/// <summary>
/// Gets the data source item.
/// </summary>
/// <value>The data source item.</value>
public Sitecore.Data.Items.Item DataSourceItem
{
get
{
if (this.dataSource == null)
{
if (this.paramHelper.DataSourceItem != null)
{
this.dataSource = this.paramHelper.DataSourceItem;
}
else
{
this.dataSource = Sitecore.Context.Item;
}
}
return this.dataSource;
}
}
/// <summary>
/// Sets the data source.
/// </summary>
/// <value>The data source.</value>
public string DataSource
{
set
{
this.dataSource = Sitecore.Context.Database.Items[value];
}
}Page_Load:
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/></param>
protected void Page_Load(object sender, EventArgs e)
{
if (this.Parent is Sitecore.Web.UI.WebControls.Sublayout)
{
this.paramHelper = new SublayoutParamHelper(this, true);
}
if (this.paramHelper != null)
{
correctItem = paramHelper.DataSourceItem;
}从这里开始,您可以在您的correctItem中加载Sub。希望这能有所帮助。希望我能充分理解你的问题。
https://stackoverflow.com/questions/5963858
复制相似问题