in
Forums
Blogs
Files
Devexpress.Com
Client Center
Support Center
DevExpress Channel

Sample code for embedding an action container in a web detail view

Last post 8/28/2008 5:01 AM by Dan (Developer Express). 10 replies.
Page 1 of 1 (11 items)
Sort Posts:
Previous Next
  • 8/26/2008 4:41 PM

    Sample code for embedding an action container in a web detail view

    For all I know, the following code may have disastrous side effects. However, it allows me to put action containers anywhere in my detail views just by creating items of the EmbeddedActionContainer type in the application model. You need to ensure that you assign the actions that you want to embed into the detail view to a unique category. Then set the ContainerId of your EmbeddedActionContainer detail view item to the name of this category. The view controller for your detail view needs to look something like:

    public partial class MyDetailViewController : ViewController

    {

        private EmbeddedActionContainerHelper embeddedActionHelper;

     

        public MyDetailViewController()

        {

            InitializeComponent();

            RegisterActions(components);

            embeddedActionHelper = new EmbeddedActionContainerHelper(this);

        }

     

        private void MyDetailViewController_Activated(object sender, EventArgs e)

        {

            embeddedActionHelper.CurrentDetailView = (DetailView)View;

        }

     

        private void MyDetailViewController_Deactivating(object sender, EventArgs e)

        {

            embeddedActionHelper.CurrentDetailView = null;

        }

    }

    You also need to add the following code to your Module.cs file:

    public override Schema GetSchema()

    {

        return new Schema(new DictionaryXmlReader().ReadFromString(

            EmbeddedActionContainer.EmbeddedActionContainerAttributes));

    }

    It should be easy to produce a Winows forms version as well, but I don't have a requirement for this at the moment.

    This code comes with absolutely no warranties whatsoever and hasn't been particularly well tested either.

    EmbeddedActionContainer.cs:

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    using DevExpress.ExpressApp;

    using DevExpress.ExpressApp.Actions;

    using DevExpress.ExpressApp.Editors;

     

    namespace MySolution.Module.Editors

    {

        public abstract class EmbeddedActionContainer : DetailViewItem

        {

            public EmbeddedActionContainer(Type objectType, DictionaryNode info)

                : base(info, objectType)

            {

                Actions = new List<ActionBase>();

                if (info != null)

                    ContainerId = info.GetAttributeValue("ContainerId");

            }

     

            public List<ActionBase> Actions { get; private set; }

     

            public string ContainerId { get; private set; }

     

            public const string EmbeddedActionContainerAttributes =

                @"<Element Name=""Application"">

                    <Element Name=""Views"">

                      <Element Name=""DetailView"" >

                        <Element Name=""Items"" >

                          <Element Name=""EmbeddedActionContainer"" >

                            <Attribute Name=""ContainerId"" IsNewNode=""True"" />

                          </Element>

                        </Element>

                      </Element>

                    </Element>

                  </Element>";

        }

     

        public class EmbeddedActionContainerHelper

        {

            private Dictionary<string, EmbeddedActionContainer> actionContainers;

            private ViewController controller;

            private DetailView currentDetailView;

     

            public EmbeddedActionContainerHelper(ViewController viewController)

            {

                controller = viewController;

                actionContainers = new Dictionary<string, EmbeddedActionContainer>();

            }

     

            public DetailView CurrentDetailView

            {

                get { return currentDetailView; }

                set

                {

                    if (currentDetailView != value)

                    {

                        // Cleanup the current view, if any

                        if (currentDetailView != null)

                        {

                            foreach (KeyValuePair<string, EmbeddedActionContainer> item in actionContainers)

                            {

                                int i = 0;

                                while (i < item.Value.Actions.Count)

                                {

                                    if (item.Value.Actions[ i ].Controller == controller)

                                        item.Value.Actions.RemoveAt(i);

                                    else

                                        i++;

                                }

                            }

                        }

     

                        // Setup the new view, if any

                        currentDetailView = value;

                        actionContainers.Clear();

                        if (currentDetailView != null)

                        {

                            // First identify all the embedded action containers and keep track of them

                            // by their ContainerId.

                            foreach (DetailViewItem item in currentDetailView.Items)

                            {

                                if (item is EmbeddedActionContainer)

                                {

                                    actionContainers[((EmbeddedActionContainer)item).ContainerId] =

                                        (EmbeddedActionContainer)item;

                                }

                            }

     

                            // Then add all actions from the view controller with categories matching

                            // the container IDs of the embedded action controllers.

                            foreach (ActionBase action in controller.Actions)

                            {

                                EmbeddedActionContainer container;

                                if (actionContainers.TryGetValue(action.Category, out container))

                                    container.Actions.Add(action);

                            }

                        }

                    }

                }

            }

        }

    }

    WebEmbeddedActionController.cs:

    using System;
    using System.Collections.Generic;

    using System.Text;

    using System.Web.UI.WebControls;

     

    using DevExpress.ExpressApp;

    using DevExpress.ExpressApp.Editors;

    using DevExpress.ExpressApp.Web.Templates.ActionContainers;

     

    using MySolution.Module.Editors;

     

    namespace MySolution.Module.Web.Editors

    {

        [DetailViewItemName("EmbeddedActionContainer")]

        public class WebEmbeddedActionContainer : EmbeddedActionContainer

        {

            public WebEmbeddedActionContainer(Type objectType, DictionaryNode info)

                : base(objectType, info) { }

           

            protected override object CreateControlCore()

            {

                HorizontalActionContainer result = new HorizontalActionContainer();

                result.ContainerId = ContainerId;

                for (int i = 0; i < Actions.Count; i++)

                    result.Register(Actions[ i ]);

                return result;

            }

        }

    }

     

  • 8/26/2008 6:06 PM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Andrew, I seem to always learn something from your posts.  Thanks so much for sharing your code!

    -- Alex Hoffman
    expressapp.blogspot.com

     

  • 8/26/2008 6:27 PM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Hi Andrew,
     
    Sounds intriguing. Do you have any screenshots of what you have done with this?
     
    Thanks,
     
    Jascha
  • 8/26/2008 6:27 PM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Apologies - duplicate post
  • 8/27/2008 5:36 AM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Jascha:

     Sounds intriguing. Do you have any screenshots of what you have done with this?

     

    I haven't looked at the properties of the HorizontalActionContainer yet, so the actions are right on top of each other, but here is a screenshot showing two dynamically populated SingleChoiceActions embedded in a detail view. The action container seems to behave exactly as it would if it were anywhere else on the page (actions appear, dispappear, update correctly, etc.). 

    Embedded Action Containers

  • 8/27/2008 6:08 AM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Ingenious. I have been wondering how to do that for a while and that might just be the ticket.
     
    I hope that DX give us an opinion on this potentially very elegant technique... Morning Dennis?
    "Andrew Klopper" wrote in message news:232584@community.devexpress.com...

    Jascha:

     Sounds intriguing. Do you have any screenshots of what you have done with this?

     

    I haven't looked at the properties of the HorizontalActionContainer yet, so the actions are right on top of each other, but here is a screenshot showing two dynamically populated SingleChoiceActions embedded in a detail view. The action container seems to behave exactly as it would if it were anywhere else on the page (actions appear, dispappear, update correctly, etc.). 

    Embedded Action Containers



    http://community.devexpress.com//forums/p/68369/232584.aspx#232584

  • 8/27/2008 6:47 AM In reply to

    Re: Sample code for embedding an action container in a web detail view

    A small fix: In WebEmbeddedActionContainer.CreateControlCore(), set result.CssClass="HContainer" to space the actions correctly.

    I have also made some small modifications to the web layout manager to allow me to dynamically show/hide layout items dynamically just by modifying a new "Visibile" attribute that I added to detail view layout items in the application model. Again, I haven't done the equivalent for the Windows forms version, but the base code is platform independent. I will happily post it if DevExpress is OK with me posting a diff with the original layout manager code. I know they don't like seeing their code posted in forums, though.

  • 8/27/2008 9:27 AM In reply to

    Re: Sample code for embedding an action container in a web detail view

    I am excited too, this IMHO should work out of the box. 
    Could you create a suggestion, so that we can track it?
     
    regards, Robert
     
    PS: Anbody out there who can translate this to Winforms? - I can't - shame on me ;-)
     
  • 8/28/2008 4:05 AM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Hello,
     
    There are similar entries in our Support Center: 
    - "PropertyEditors: Ability to add some functionality to a property editor: one/two additional actions" at http://www.devexpress.com/issue=AS10841
    - "ActionContainers: Introduce an ability to place an action anywhere in a detail view" at http://www.devexpress.com/issue=S20125

    --
    Thanks,
    Dan.
     
    R&D, .NET Team, Developer Express Inc.
     
    PS. Please don't reply to me directly by e-mail unless it is specifically
    requested. If you wish to receive direct assistance from our Support Team,
    use Support Center at http://www.devexpress.com/Support/Center
     
    Please take a look at our Knowledge Base at: http://www.devexpress.com/kb.
    It contains useful information on our products that may save your time.
  • 8/28/2008 4:14 AM In reply to

    Re: Sample code for embedding an action container in a web detail view

    Hi Dan,
     
    Thanks for the info. What I (and others) would like to know is whether the technique Andrew has come up with here is ok to use. I.e. there are no problematic side effects of doing it this way etc.?
     
    Thanks,
     
    Jascha
    "Dan (Developer Express)" wrote in message news:232743@community.devexpress.com...
    Hello,
     
    There are similar entries in our Support Center: 
    - "PropertyEditors: Ability to add some functionality to a property editor: one/two additional actions" at http://www.devexpress.com/issue=AS10841
    - "ActionContainers: Introduce an ability to place an action anywhere in a detail view" at http://www.devexpress.com/issue=S20125

    --
    Thanks,
    Dan.
     
    R&D, .NET Team, Developer Express Inc.
     
    PS. Please don't reply to me directly by e-mail unless it is specifically
    requested. If you wish to receive direct assistance from our Support Team,
    use Support Center at http://www.devexpress.com/Support/Center
     
    Please take a look at our Knowledge Base at: http://www.devexpress.com/kb.
    It contains useful information on our products that may save your time.


    http://community.devexpress.com//forums/p/68369/232743.aspx#232743

  • 8/28/2008 5:01 AM In reply to

    Re: Sample code for embedding an action container in a web detail view