Get rendering parameters when multiple sublayouts of the same type are on the page(当页面上有多个相同类型的子布局时,获取呈现参数)
问题描述
我需要从子布局以编程方式获取呈现参数。目前我是这样做的:
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);
此解决方案来自this question,在我的页面上有两个相同类型的子布局之前,它一直工作得很好。当这种情况明显发生时,renderings.FirstOrDefault(r => r.RenderingItem.InnerItem["Path"] == sublayout.Path);
始终返回与两个子布局的子布局路径匹配的第一个呈现参数。
我如何区分它们?我看不到可以用来把它们绑在一起的东西!
编辑:
为了清楚起见,我在演示>详细信息中添加子布局,然后在单击控件时设置"控件属性"窗口中的字段。我有一个名为Module Source的字段,它返回的结果总是相同的-它总是作为顺序中最高的一个填充。每个子布局的值肯定不同,但我无法从渲染中获取它们。
推荐答案
不确定我是否遗漏了什么。但是您可以直接在子布局上获取子布局渲染参数。我在用于所有Sitecore子布局的基本子布局上使用以下内容-在多次插入的同一个子布局上呈现参数没有任何问题:)
protected Sitecore.Web.UI.WebControls.Sublayout CurrentSublayout
{
get
{
Control c = Parent;
while (c != null && !(c is Sitecore.Web.UI.WebControls.Sublayout))
{
c = c.Parent;
if (c == null)
break;
}
return c as Sitecore.Web.UI.WebControls.Sublayout;
}
}
protected NameValueCollection CurrentParameters
{
get
{
if (CurrentSublayout == null)
return null;
NameValueCollection parms = WebUtil.ParseUrlParameters(CurrentSublayout.Parameters);
var sanitizedValues = new NameValueCollection();
for (int i = 0; i < parms.Count; i++)
{
if (!string.IsNullOrEmpty(parms[i]))
sanitizedValues.Add(parms.Keys[i], parms[i]);
}
return sanitizedValues;
}
}
这篇关于当页面上有多个相同类型的子布局时,获取呈现参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当页面上有多个相同类型的子布局时,获取呈现参数


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01