Sitecore:如何使用代码隐藏中的子布局参数? [英] Sitecore: How to use sublayout parameters from codebehind?

查看:22
本文介绍了Sitecore:如何使用代码隐藏中的子布局参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从子布局代码隐藏的参数"字段(第二个屏幕截图)中获取值?

我知道我可以在将渲染(特别是子布局)添加到项目的演示细节时获取/设置参数,正如此处所述(Sitecore 6 - 使用参数).

I understand I can get/set parameters on a rendering (specifically sublayout) when it is added to the presentation details of an item, just as described here (Sitecore 6 - using parameters).

但是我想使用布局定义项中的参数字段.在属于布局定义的文件的代码隐藏中,我可以将父布局转换为子布局,并且该对象也有一个 .Parameters 属性,但是它不包含我期望的值.

However I would like to use the parameters field from the layout definition item. In the codebehind of the file belonging to to layout definition I can cast the parent to a sublayout and that object also has a .Parameters property, however this doesn't contain the values I'd expect.

这是控制代码隐藏中的Page_Load方法:

This is the Page_Load method in the control code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    var sublayout = ((Sublayout)this.Parent);
    string rawParameters = Attributes["sc_parameters"];
    NameValueCollection parameters =
      Sitecore.Web.WebUtil.ParseUrlParameters(rawParameters); 
      //parameters contains values from "Additional parameters (first screenshot)

      //I do not know the sublayout item id or sublayout path, so how do I get
      //the values from the second screenshot?
}

Doublecheck 还是不行,只显示附加参数:

推荐答案

您可以获取子布局上定义的参数,但有点啰嗦.您需要先找到正确的渲染项,然后从中检索参数

You can get the parameters defined on the sublayout but it's a bit long winded. You need to find the correct rendering item first and from there retrieve the parameters

   var sublayout = ((Sublayout)this.Parent);
   //Get all rendering
   var renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);

   //Get the first rendering that matches the current sublayout's path
   var sublayoutRendering = renderings.FirstOrDefault(r => r.RenderingItem.InnerItem["Path"] == sublayout.Path);

   if (sublayoutRendering != null)
         Response.Write(sublayoutRendering.RenderingItem.Parameters);

布局详情"中设置的参数可以使用Mark的方式获取参数

You can use Mark's way to get the parameters for the parameters set in the "Layout Details"

上述解决方案将起作用,但它非常脆弱,并且取决于将来可能会改变的 sitecore 内部结构.我不建议你在生产中使用它.必须有更好的方法来实现您想要的.

The above solution will work but it's very fragile and depends on sitecore internals that might change in the future. I wouldn't recommend you go with it in production with it. There must be a better way to achieve what you want.

这篇关于Sitecore:如何使用代码隐藏中的子布局参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆