sending data from one sublayout to another in sitecore

泄露秘密 提交于 2020-01-12 08:28:13

问题


I'm having a hard time building a filtering system in Sitecore 7.

I have 2 sublayouts, on the same level of the page.

Sublayout A is a sidebar that contains a list of checkboxes and has an event that populates a List with the selected values. Sublayout B displays a set of items.

What I would like to do, is send the populated List from sublayout A to sublayout B in order to filter the items list based on what the user selected. I was able to do this by passing the data through Session, but this is not an optimal way of handling that data.

I've tried defining a property for sublayout A and loading the list there, but I can't get the exact instance of sublayout A from sublayout B in order to read the populated property. Also, trying to Page.FindControl("IdOfSomeElementFromSublayoutA") always returns null in Sublayout B. Even though I've casted Page as the .aspx page that contains both Sublayouts.

I'm using Sitecore 7 Update 2.

Thanks a lot for your time.


回答1:


The best way to do this is by raising (and subscribing to) events using the Sitecore.Events.Event class. Your sidebar sublayout would raise an event using something like the following within a button's click event handler:

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

then in the other sublayout you would need to have the following set up in order to handle the event:

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
        }
    }
}

Note: it is important to keep the Page_Unload code there otherwise you will see the EventHandler method being called multiple times.



来源:https://stackoverflow.com/questions/19495679/sending-data-from-one-sublayout-to-another-in-sitecore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!