在sitecore中将数据从一个子布局发送到另一个子布局

时间:2013-10-21 13:19:44

标签: c# asp.net sitecore sitecore7

我很难在Sitecore 7中构建过滤系统。

我在页面的同一级别有2个子布局。

子布局A是一个侧边栏,其中包含一个复选框列表,并且具有使用所选值填充列表的事件。 子布局B显示一组项目。

我想要做的是将填充的List从子布局A发送到子布局B,以便根据用户选择的内容过滤项目列表。 我能够通过Session传递数据来做到这一点,但这不是处理数据的最佳方式。

我已经尝试为子布局A定义属性并在那里加载列表,但我无法从子布局B获取子布局A的确切实例以便读取填充的属性。 此外,尝试Page.FindControl(“IdOfSomeElementFromSublayoutA”)始终在Sublayout B中返回null。尽管我已将Page转换为包含两个子布局的.aspx页面。

我正在使用Sitecore 7 Update 2.

非常感谢你的时间。

1 个答案:

答案 0 :(得分:5)

执行此操作的最佳方法是使用Sitecore.Events.Event类引发(和订阅)事件。您的侧边栏子布局会在按钮的单击事件处理程序中使用以下内容引发事件:

Sitecore.Events.Event.RaiseEvent("YourEventName", new YourEventArgsClass { Property = "SomeValue" });

然后在其他子布局中,您需要进行以下设置才能处理事件:

public partial class YourOtherSublayout : System.Web.UI.UserControl
{
    private System.EventHandler eventHandlerRef;

    protected void Page_Load(object sender, EventArgs e)
    {
        eventHandlerRef = EventHandlerMethod;
        Sitecore.Events.Event.Subscribe("YourEventName", eventHandlerRef);
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        if (eventHandlerRef != null)
        {
            Sitecore.Events.Event.Unsubscribe("YourEventName", eventHandlerRef);
        }
    }

    private void EventHandlerMethod(object sender, EventArgs e)
    {
        if (e != null)
        {
            //do stuff here
        }
    }
}

注意:保持Page_Unload代码非常重要,否则您将看到多次调用EventHandler方法。