Sitecore:如何使用代码隐藏中的子布局参数?

发布于 2024-11-05 19:53:09 字数 1385 浏览 0 评论 0原文

如何从子布局后面的代码中的“参数”字段(第二个屏幕截图)获取值?

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

布局实例参数

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

布局定义参数

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

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 仍然没有'不起作用,仅显示其他参数

Step 1 - Enter parameters

步骤 2 - 将子布局 + 参数添加到演示文稿

步骤 3 - 在子布局上显示参数

步骤 4 - 验证结果

How do I get the values from the "Parameters" field (second screenshot) in the code-behind of the sublayout?

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).

layout instance 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.

layout definition parameters

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 still doesn't work, only additional parameters are shown:

Step 1 - Enter parameters

Step 2 - Add sublayout + parameters to presentation

Step 3 - Display parameters on sublayout

Step 4 - Validate result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-11-12 19:53:09

像这样:

var sublayout = ((Sublayout)this.Parent);
NameValueCollection nvc = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);

这是一篇博客文章,使使用扩展方法更容易做到

这是一个 Sitecore 的共享源模块,它也将其包装在一个类中 。它是由 Sitecore USA 首席技术官 John West 编写的。

Like this:

var sublayout = ((Sublayout)this.Parent);
NameValueCollection nvc = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);

Here's a blog post that make it easier to do with extension methods.

Here's a shared source module for Sitecore that wraps this up in a class as well. It was written by John West, CTO of Sitecore USA.

故事↓在人 2024-11-12 19:53:09

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

   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的方式获取“布局详细信息”中设置的参数

编辑:上述解决方案可以工作,但它非常脆弱并且取决于sitecore内部这在未来可能会改变。我不建议您在生产中使用它。一定有更好的方法来实现你想要的。

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

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

EDIT: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文