+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Command Panel: Creating command panel from Custom Control

by Ivan Osmak

[This post is part of the developer's manual preview published on this blog. You can find temporary TOC here.]
 

In this article we are going to cover the final and most flexible option for creating a command panel: creating a command panel from a custom control. The more experienced Sitefinity developers will find this approach very familiar, since this is basically the way command panels have been created since the release of Sitefinity 3.0.

 


Creating a command panel class


Similarly to the way we have been creating User Control based command panel, we will start by creating the custom control which has to implement ICommandPanel interface. If we wish to have access to CreateRootCommand method - with which we can construct command calls to load Views, we should inherit from the ViewModeControl class. The fundamentals for loading the template and working with controls are exactly the same as for any View.

 

The code for a Custom Control based command panel for our sample books module looks like following:
using System.Web.UI.WebControls; 
using Telerik.Cms.Web.UI; 
using Telerik.Web; 
 
namespace Samples.Books.WebControls 
    /// <summary> 
    /// Custom control which is used for command panel 
    /// </summary> 
    public class BooksCommandPanel : ViewModeControl<BooksControlPanel>, ICommandPanel 
    { 
        /// <summary> 
        /// Initializes a new instance of the <see cref="BooksCommandPanel"/> class. 
        /// </summary> 
        /// <param name="ctrlPnl">The CTRL PNL.</param> 
        public BooksCommandPanel(IControlPanel ctrlPnl) 
        { 
            this.ctrlPnl = ctrlPnl; 
        } 
 
        /// <summary> 
        /// Gets or sets the path to a custom layout template for the control. 
        /// </summary> 
        /// <remarks> 
        /// We are overriding this property only to add attribute to it. The attribute will be used in future 
        /// for the template mapping / exporting tool. 
        /// </remarks> 
        [WebSysTemplate(BooksCommandPanel.BooksCommandPanelTemplateName, 
            "BooksCommandPanelTemplateName_Template_Desc""/Books/"false"2000-01-25")] 
        public override string LayoutTemplatePath 
        { 
            get 
            { 
                return base.LayoutTemplatePath; 
            } 
            set 
            { 
                base.LayoutTemplatePath = value; 
            } 
        } 
 
        /// <summary> 
        /// Gets the name of the embedded layout template. If the control uses layout template this 
        /// property must be overridden to provide the path (key) to an embedded resource file. 
        /// </summary> 
        /// <value>Name of the resource with embedded template</value> 
        protected override string LayoutTemplateName 
        { 
            get 
            { 
                return BooksCommandPanel.BooksCommandPanelTemplateName; 
            } 
        } 
        #region Control references 
 
        /// <summary> 
        /// Gets all books. 
        /// </summary> 
        /// <value>All books.</value> 
        protected virtual HyperLink AllBooks 
        { 
            get { return base.Container.GetControl<HyperLink>("allBooks"true); } 
        } 
 
        /// <summary> 
        /// Gets the sales. 
        /// </summary> 
        /// <value>The sales.</value> 
        protected virtual HyperLink Sales 
        { 
            get { return base.Container.GetControl<HyperLink>("sales"true); } 
        } 
        #endregion 
 
        /// <summary> 
        /// Initializes all controls instantiated in the layout container. This method is called at appropriate time for setting initial values and subscribing for events of layout controls. 
        /// </summary> 
        /// <param name="viewContainer">The control that will host the current view.</param> 
        protected override void InitializeControls(System.Web.UI.Control viewContainer) 
        { 
            base.InitializeControls(viewContainer); 
 
            this.AllBooks.NavigateUrl = CreateRootViewCommand(typeof (BooksControlPanel), typeof (BooksListView)); 
            this.Sales.NavigateUrl = CreateRootViewCommand(typeof (BooksControlPanel), typeof (BooksSalesView)); 
        } 
 
        /// <summary> 
        /// Refreshes the command panel information. 
        /// </summary> 
        /// <requirements> 
        ///             This method could be used to refresh the command panel information. For example 
        ///             when there is some change in the ControlPanel which reflects on the information of the 
        ///             CommandPanel. 
        ///             </requirements> 
        /// <example> 
        ///                 You can refer to <see cref="T:Telerik.Web.ICommandPanel">ICommandPanel</see> interface for more 
        ///                 complicated example implementing the whole 
        ///                 <see cref="T:Telerik.Web.ICommandPanel">ICommandPanel</see> interface. 
        /// </example> 
        public void Refresh() 
        { 
            this.ChildControlsCreated = false
        } 
 
        /// <summary> 
        /// Reference to the control panel tied to the command panel instance. 
        /// </summary> 
        /// <remarks> 
        ///             This property is used for communication between the command panel and its control 
        ///             panel. 
        /// </remarks> 
        /// <example> 
        ///                 You can refer to <see cref="T:Telerik.Web.ICommandPanel">ICommandPanel</see> interface for more 
        ///                 complicated example implementing the whole 
        ///                 <see cref="T:Telerik.Web.ICommandPanel">ICommandPanel</see> interface. 
        /// </example> 
        public IControlPanel ControlPanel 
        { 
            get { return this.ctrlPnl; } 
        } 
 
        private IControlPanel ctrlPnl; 
 
        private const string BooksCommandPanelTemplateName = 
            "Samples.Books.Resources.ControlTemplates.Backend.BooksCommandPanel.ascx"
    } 
 
The implementation is trivial enough that it does not need any further explanation.

 

Loading Custom Control based command panel into a control panel


So far we have seen, that when we use automatic command generation, Sitefinity takes information provided in CreateViews method to generate commands. Then, we’ve seen that we can override the CreateStandardCommandPanels method to manually create command panels and commands. In the previous article, we’ve learned that by overriding CreateCommandPanelsFromUserControls we can load command panels built as User Controls. Finally, by overriding CreateCustomCommandPanels method we are able to load command panels that we’ve developed as Custom Controls. To load the control we’ve built in the first part of this article we can use following code:
/// <summary> 
/// Creates the custom command panels. 
/// </summary> 
/// <param name="viewMode">The view mode.</param> 
/// <param name="list">The list.</param> 
protected override void CreateCustomCommandPanels(string viewMode, System.Collections.Generic.List<Telerik.Web.ICommandPanel> list) 
            list.Add(new BooksCommandPanel(this)); 
 
The viewMode parameter we receive represents the currently loaded view of our View hierarchy and can be used for conditional command panel loading as explained in this article. The second parameter is a list of all command panels used by the control panel, and we can load our command panel by simply adding a new instance of the control we’ve created in the first part of this article.

Leave a comment