1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Extending the CommandPanel of the Products module

Extending the CommandPanel of the Products module

5 posts, 0 answered
  1. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    23 Dec 2008
    Link to this post

    Hi,

    I'd like to extend the Products module to add an import function.  I'm looking for information or a tutorial on how to add a new CommandPanel button and wire that to the ControlPanel so that I can put my code to do the import.  I have pieces of what I need, but I'm missing something conceptually here.  It seems like I need to:

    1.  Add the public property and local member variable for the ProductImportButton to the public Container class:

     public class Container : GenericContainer<CommandPanel>

    IButtonControl productImportButton;  
     
    public IButtonControl ProductImportButton  
    {  
        get  
        {  
            if (productImportButton == null)  
                productImportButton = (IButtonControl)FindRequiredControl<Control>("productImportButton");  
            return productImportButton;  
        }  
    }  
     



    2.  Add the link button to the CreateChildControls method:

    // set up the behavior of ProductImportButton  
    container.ProductImportButton.CommandName = "ProductImportView";  
    container.ProductImportButton.Command += button_Command;  
    if (container.ProductImportButton is LinkButton && (ctrlPnl.Mode == Cms.Engine.WebControls.Admin.ControlPanel.Modes.ProductImport))  
        ((LinkButton)container.ProductImportButton).CssClass = "sel";  
     

    3.  And then add an action for the button:

    case "ProductImportView":  
        ctrlPnl.Mode = 100;  
        ctrlPnl.Refresh();  
        break;  
     


    But I don't see how I can add items to the Modes enum...I guess I'm supposed to override the public enum from the base class in the Products ControlPanel.cs file, like:

    public override enum Modes
    {
     List = 0,
     Insert = 1,
     View = 2,
     Edit = 3,
     History = 4,
     Permissions = 5,
     CommentsView = 6,
     CommentsEdit = 7,
     Tags = 8,
     Categories = 9,
     ProductImport = 100,
    }

    and then update items 2. and 3. above to use the enum. 

     

    But more importantly, I also don't see how I can relate any particular mode to a specific action...where is this set up?  For example, how does setting:

    ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.List;

    cause the ControlPanelList.ascx file to be loaded? 

    Enquiring minds want to know!  ;^)

    Thanks in advance,
    Mike

  2. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    23 Dec 2008
    Link to this post
    I should add that obviously my use of the enum above is incorrect...which is why I need to know how I can extend the existing Cms.Engine.WebControls.Admin.ControlPanel.Modes

    Mike
  3. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    31 Dec 2008
    Link to this post
    Hello Mike,

    In this case:

     case "ProductImportView":   
                ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.ProductImportView;   
                ctrlPnl.Refresh();   
                break;  

    We switch the mode (case ProductImportView) and refresh the command panel.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    31 Dec 2008
    Link to this post
    I think I know how to get the switch statement working (though I'm not sure I understand the magic behind the scenes that adds my new control).

    But my problem is that Telerik.Cms.Engine.WebControls.Admin.ControlPanel.Modes does not contain a definition for ProductImportView. 

    How do I fix that?

    I started looking at he Contacts module example, and I'm beginning to think I'm going to have to rewrite a lot of the Products module in order to be able to add new controls to the ControlPanel.

    Regards,
    Mike Sharp
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    08 Jan 2009
    Link to this post
    Hi Mike,

    You need to create your own custom enum and you do not have to override the default one.

    Example: In ControlPanel.cs we add new enum ProductModes.

       public enum ProductModes 
            { 
                
                ImportProductsView = 1, 
                None = 2, 
     
            } 

    After that we add a new property in the same cs.

    namespace Telerik.Samples.Products.WebControls.Admin 
        /// <summary> 
        /// Control panel command. This class only needs to implement CommandPanels property, everything 
        /// else is inherited from base ControlPanel. 
        /// </summary> 
        public class ControlPanel : Telerik.Cms.Engine.WebControls.Admin.ControlPanel 
        { 
            public ProductModes ProductMode 
            { 
                get 
                { 
                    return productMode; 
                } 
                set 
                { 
                    productMode = value; 
                } 
            }
              ProductModes productMode;

    Then, in CommandPanel.cs we add our custom Modes as below. As you see we use our custom mode and custom property ProductMode here.

            protected virtual void button_Command(object sender, CommandEventArgs e) 
            { 
                // based on the command name, set the Mode of ControlPanel   
                ControlPanel ctrlPnl = ((ControlPanel)base.ControlPanel); 
                switch (e.CommandName) 
                { 
                    case "ContentView"
                        ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.List; 
                        ctrlPnl.ProductMode = Telerik.Samples.Products.WebControls.Admin.ControlPanel.ProductModes.None; 
                        ctrlPnl.Refresh(); 
                        break
                    case "CategoriesView"
                        ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Categories; 
                        ctrlPnl.ProductMode = Telerik.Samples.Products.WebControls.Admin.ControlPanel.ProductModes.None; 
                        ctrlPnl.Refresh(); 
                        break
                    case "TagsView"
                        ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Tags; 
                        ctrlPnl.ProductMode = Telerik.Samples.Products.WebControls.Admin.ControlPanel.ProductModes.None; 
                        ctrlPnl.Refresh(); 
                        break
                    case "CommentsView"
                        ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.CommentsView; 
                        ctrlPnl.ProductMode = Telerik.Samples.Products.WebControls.Admin.ControlPanel.ProductModes.None; 
                        ctrlPnl.Refresh(); 
                        break
                    case "PermissionView"
                        ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Permissions; 
                        ctrlPnl.ProductMode = Telerik.Samples.Products.WebControls.Admin.ControlPanel.ProductModes.None; 
                        ctrlPnl.Refresh(); 
                        break
                    case "ImportProducts"
                        ctrlPnl.ProductMode = Telerik.Samples.Products.WebControls.Admin.ControlPanel.ProductModes.ImportProductsView; 
                        break
                } 
                 

    We need to change the behavior of our ContentViewButton and set the event that occurs when the button is clicked and set the command name of ImportProducts.


    // set up the behavior of ContentViewButton 
                    this.container.ContentViewButton.CommandName = "ContentView"
                    this.container.ContentViewButton.Command += new CommandEventHandler(button_Command); 
                    if (this.container.ContentViewButton is LinkButton && 
                        (ctrlPnl.Mode == Cms.Engine.WebControls.Admin.ControlPanel.Modes.List || 
                         ctrlPnl.Mode == Cms.Engine.WebControls.Admin.ControlPanel.Modes.View || 
                         ctrlPnl.Mode == Cms.Engine.WebControls.Admin.ControlPanel.Modes.Edit || 
                         ctrlPnl.Mode == Cms.Engine.WebControls.Admin.ControlPanel.Modes.History)) 
                        ((LinkButton) this.container.ContentViewButton).CssClass = "sel"
     
                    this.container.ImportProductsButton.Command += new CommandEventHandler(button_Command); 
                    this.container.ImportProductsButton.CommandName = "ImportProducts"
     
                    this.Controls.Add(this.container); 



    In our ControlTemplate we add a new Link button and Label for this mode.(~\Sitefinity\Admin\ControlTemplates\Products\CommandPanel.ascx)


    <dt><asp:LinkButton ID="importProductsButton" Text="Import products" runat="server" /></dt
    <dd><asp:Literal ID="Literal4" runat="server" Text="Click here to import products"></asp:Literal></dd
     

    Finally, we override CreateChildControls and load our custom interface.

     protected override void CreateChildControls() 
            { 
                switch (ProductMode) 
                { 
                    case ProductModes.ImportProductsView: 
                        Label testLabel = new Label(); 
                        testLabel.Text = "Import products test UI"
                        this.Controls.Add(testLabel); 
                        break
                    case ProductModes.None: 
                        base.CreateChildControls(); 
                        break
                } 
            } 

    I hope this helps.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
5 posts, 0 answered