首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从子布局代码后面获取子布局项?

如何从子布局代码后面获取子布局项?
EN

Stack Overflow用户
提问于 2011-05-11 12:00:09
回答 4查看 7.2K关注 0票数 1

我正在尝试获取代码背后的子布局项(或项id)。,这有可能吗?

更新:

我指的不是Datasource项或当前项,而是Sublayout呈现定义项。这与子布局文件有1到1的关系.

代码隐藏文件:

代码语言:javascript
复制
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?
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-11 12:40:34

此页将解释细节,但下面是您需要的代码:

代码语言:javascript
复制
Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);

更新:

您可能可以通过使用数据库来通过ID获取呈现项本身:

代码语言:javascript
复制
Sitecore.Context.Database.GetItem(((Sublayout)Parent).RenderingID);
票数 5
EN

Stack Overflow用户

发布于 2011-08-26 13:57:54

不需要循环遍历控件或根据控件名称检查子布局。

完成任务的正确方法是遵循以下步骤:

  1. 获取当前项的LayoutDefinition。
  2. 使用LayoutDefinition,获取您感兴趣的子布局的RenderingDefinition。
  3. 根据完整的列表获取子布局的索引
  4. 使用索引检索子布局的RenderingReference
  5. 使用RenderingReference获取对.NET控件的引用。

以下是实现你的目标的守则:

代码语言:javascript
复制
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();

现在,您有了对点网控件的引用。只需将其类型强制转换到相应的控件,以获得对对象的完全访问。

票数 3
EN

Stack Overflow用户

发布于 2011-05-11 12:17:20

要获取用于将特定控件放置到页面中的项,可以使用一个名为的共享源模块。模块可以找到这里

如果要检索该项,可以考虑使用以下设置:

道具:

代码语言:javascript
复制
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:

代码语言:javascript
复制
/// <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。希望这能有所帮助。希望我能充分理解你的问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5963858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档