2011-05-11 51 views
1

我试图在代码隐藏中获取子布局项目(或项目标识)。 这可能吗?Sitecore:如何从子布局代码隐藏获取子布局ITEM?

更新:

我说的不是数据源项目或当前项目,我谈论的Sublayout渲染定义项。这与子文件文件有一对一的关系。

代码隐藏文件:

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? 
    } 
} 
+0

我已经更新了我的答案。 – 2011-05-11 16:55:01

+0

请给我们一个更新。如果我回答您的问题,请将其标记为“否”,否则请更新我们的状态。谢谢。 – 2011-05-13 18:32:08

回答

5

This page将解释的细节,但在这里,你需要的代码:

Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource); 

UPDATE:

你或许可以通过使用数据库的ID来得到它得到渲染项目本身:

Sitecore.Context.Database.GetItem(((Sublayout)Parent).RenderingID); 
+0

DataSource用于其他功能。请看我更新的问题。 – ReFocus 2011-05-11 13:00:25

+0

我已经更新了我的回答 – 2011-05-11 14:04:23

+0

感谢您的思考。当然我在开发过程中已经检查过这些属性。我发现'RenderingID'不包含你认为会的GUID。无论如何,我已经与支持联系,唯一的方法是循环通过项目渲染并将其与ascx文件名匹配......完全没用,因为我在多个呈现项目之间共享一个ascx文件(这不是'Sitecore Best练习'无论如何..) – ReFocus 2011-05-18 08:05:48

2

要获得用来放置特定控制的页面,您可以使用一个共享源模块称为Sublayout参数辅助的项目。 该模块可发现here

如果你想找回你可以考虑使用以下设置项:

道具:

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; 
} 

从那里你有你的正确的项目加载。 希望这有助于。希望我能够很好地理解你的问题。

+0

我的问题没有正确定义。请参阅最新的问题,我不是在谈论数据源项目或“当前项目”。我在谈论Sublayout渲染定义项目。 – ReFocus 2011-05-11 13:01:41

3

不需要循环控件或通过控件n检查子布局AME。

去了解任务的正确方法是通过以下步骤:

  1. 获取LayoutDefinition为当前项目。
  2. 使用LayoutDefinition,让你感兴趣的sublayout的RenderingDefinition。
  3. 获取sublayout的指数对完整列表
  4. 使用索引来检索RenderingReference为sublayout
  5. 使用RenderingReference以获得对.NET控件的引用。

下面是代码实现自己的目标:

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(); 

现在你有关于你点网控制。只需将其转换为相应的控件即可完全访问该对象。

0

这里是一个一行,让你的Sublayout项目:

Sitecore.Context.Page.Renderings.Select(r => Sitecore.Context.Database.Items[r.RenderingID]) 
    .Where(item => item["Path"] == ((Sublayout)this.Parent).Path).Single(); 

请记住,如果你因为某些原因具有相同路径的多个Sublayouts这将失败。