Sitecore:如何使用代码隐藏中的子布局参数?
如何从子布局后面的代码中的“参数”字段(第二个屏幕截图)获取值?
我知道我可以在渲染(特别是子布局)上获取/设置参数添加到项目的演示详细信息,正如此处所述 (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 仍然没有'不起作用,仅显示其他参数
:
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).
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.
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样:
这是一篇博客文章,使使用扩展方法更容易做到。
这是一个 Sitecore 的共享源模块,它也将其包装在一个类中 。它是由 Sitecore USA 首席技术官 John West 编写的。
Like this:
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.
您可以获取子布局上定义的参数,但这有点啰嗦。您需要首先找到正确的渲染项目,然后从那里检索参数
您可以使用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
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.