0

我正在尝试在 Sitecore 中实现 Nick Wesselman 的动态占位符技术。我正在使用 sitecore 6.5 和 asp.net。

http://www.techphoria414.com/Blog/2011/August/Dynamic_Placeholder_Keys_Prototype http://www.techphoria414.com/Blog/2012/May/Sitecore_Page_Editor_Unleashed

我使用了在 Sitecore_Page_Editor_Unleashed BLOG 中找到的源代码

所有管道似乎都已就位并正在工作。但是在动态占位符控件中,尽管控件上有五个动态占位符,但以下代码位返回 0(零)

Stack<Placeholder> stack = Switcher<Placeholder, PlaceholderSwitcher>.GetStack(false);

为了隔离问题,我创建了一个非常简单的 sitecore 实例。1 个布局和 1 个子布局。

在代码隐藏子布局中,我有以下代码用于演示目的:

var list = new List<int>();
for (int i = 0; i < 5; i++)
{
    list.Add(i);
}

Repeater.DataSource = list;
DataBind();

这是 ascx/sublayout 的来源

<asp:Repeater runat="server" ID="Repeater">
     <ItemTemplate>
       <mi:DynamicKeyPlaceholder runat="server"  ID="pl" Key="place"></mi:DynamicKeyPlaceholder> 
    </ItemTemplate>
</asp:Repeater>

结果是所有五个占位符仍然具有相同的键。

该怎么办?

4

2 回答 2

1

I've never tested this solution with multiple dynamically created placeholder controls inside the same sublayout. It's designed to solve the problem of the same sublayout (with a placeholder) being placed on a page, multiple times. It works by grabbing the ID of the containing rendering and appending it to the placeholder key. Steps I would take:

  • Ensure that the sublayout containing your repeater is placed in the layout dynamically, through a placeholder. You should not be using <sc:Sublayout/> to put the sublayout on the page. This may explain why the stack is empty.
  • You will also need to databind the "Key" field on the DynamicKeyPlaceholder. Within the same sublayout, all the DynamicKeyPlaceholder controls need to have a unique Key.

Of course, the danger here is that if the data driving your repeater changes, the Key could change. You might consider re-evaluating your architecture and driving it based on placing the same sublayout multiple times (with datasources), instead of a repeater.

于 2013-03-21T17:22:25.377 回答
0

我所做的与您的要求相似...我从调用控件(希望具有动态 ph 值)创建了另一个子布局(实现动态占位符)的引用。

所以假设我的调用控件是ControlA,实现控件是ControlB。

这是ControlA 的标记

 <ControlB ID="SectionItems" runat="server" CurrentItem="<%# Container.DataItem %>" />

这是它背后的代码

 private static void SetIndexForControlBItems(RepeaterItemEventArgs e)
   {
       var controlBItems = e.Item.FindControl("ControlB") as ControlB;
       if (controlBItems != null)
          controlBItems.PropertyName = e.Item.ItemIndex.ToString();
   }

当您分配属性名称(将出现在 ControlB 中)时,您将获得分配动态 PH 的索引的值。在ControlB中,您可以执行以下操作:

 private void SetPlaceholderKeys(RepeaterItemEventArgs e)
    {
        Placeholder phPreColumnContentTopLeft = e.Item.FindControl("phColumnContentTopLeft") as Placeholder;
        if (phPreColumnContentTopLeft != null)
            phPreColumnContentTopLeft.Key = "topLeftColumnSectionItems" + e.Item.ItemIndex + PropertyName;

希望这会有所帮助

于 2013-03-25T14:16:12.813 回答