views:

412

answers:

2

I am dealing with a master page split into various content placeholders as per usual. On a number of pages I use a multiview to show and hide the content based on different view modes (view / edit / add). This means one multiview per master page content placeholder...

The trouble comes when I need to switch view modes, eg from 'View' to 'Edit'. I need to change every multiview on the page to show the corresponding information. So say 4 different content placeholders exist, with 3 different view modes - thats 12 lines of quite tedious code.

I'm wondering if there is a way to sync or link all multiviews on a page, so that when one changes - they all change accordingly? Like through some sort of master multiview?

I've thought about using the View's Activate Event, but this would still mean doing a lot of wiring up to begin with, which is what I'm trying to avoid.

C#eers!

+1  A: 

The best way to do this would be to have an event on the master page which the individual multiviews add an event handler to.

That should not need too much code, and you could avoid duplicating that code by creating a base class inheriting from MultiView for all your instances.

Rob West
A: 

Another more automatic option (but not as performant as RobW's one), would be to recursively look on the page's controls for any multiview. Something like:

ApplyToControlsRecursive(Page, c =>
{
    var multi = c as System.Web.UI.WebControls.MultiView;
    if (multi != null)
        multi.ActiveViewIndex = 1;
});
void ApplyToControlsRecursive(System.Web.UI.Control control, Action<System.Web.UI.Control> action)
{
    action(control);
    foreach (System.Web.UI.Control child in control.Controls)
    {
        ApplyToControlsRecursive(child, action);
    }
}
eglasius