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;
}
}
这篇关于当页面上有多个相同类型的子布局时,获取呈现参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当页面上有多个相同类型的子布局时,获取呈现参数
基础教程推荐
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
