Sitecore:如何使用codebehind中的sublayout参数? [英] Sitecore: How to use sublayout parameters from codebehind?
问题描述
如何从子布局背后代码的参数"字段(第二张屏幕截图)中获取值?
我了解,当将渲染(特别是子布局)添加到项目的演示文稿详细信息中时,我可以获取/设置参数,如此处所述(
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字段.在属于布局定义的文件的代码后面,我可以将父级强制转换为子布局,并且该对象还具有.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?
}
再次检查仍然无效,仅显示additional parameters
:
Doublecheck still doesn't work, only additional parameters
are shown:
推荐答案
您可以获取在子布局上定义的参数,但是有点麻烦.您需要先找到正确的渲染项目,然后从那里检索参数
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:如何使用codebehind中的sublayout参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!