More in this section

Forums / Developing with Sitefinity / NavigateHostCommand: Index was out of range

NavigateHostCommand: Index was out of range

5 posts, 1 answered
  1. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    28 May 2010
    Link to this post
    Dear Telerik Team

    I have issue when i want to implement NavigateHostCommand in my commandpanel class
    I have tried to follow the contact36 sample module but error message still appear

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Thanks in advance
    Febry Nastrioman





     
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 May 2010
    Link to this post
    Hello CafeDelMar,

    Could you share some sample code? By default this error is not thrown in the contacts module. There must be some problem in the code that you have added.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    29 May 2010
    Link to this post
    Hi Ivan

    Thanks for your quick response

    This is my sample code

    ShoppingCartControlPanel.cs
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Web.UI;
    using Telerik.Framework.Web;
    using Telerik.Web;
     
    using Telerik.Cms.Web.UI.Backend;
    using System.Collections.Generic;
    using Moduleplex.ShoppingCart.WebControls.Admin.Administrator;
    using Moduleplex.ShoppingCart.Configuration;
    using Moduleplex.ShoppingCart.Resources;
     
     
     
    namespace Moduleplex.ShoppingCart.WebControls.Admin
    {
        public class ShoppingCartControlPanel : ProviderControlPanel<ShoppingCartControlPanel>
        {
            #region Constructors
     
            /// <summary>
            /// Initializes a new instance of the <see cref="ShoppingCartControlPanel"/> class.
            /// </summary>
     
            public ShoppingCartControlPanel()
                : base(false)
            {
            }
            /// <summary>
            /// Initializes a new instance of the <see cref="ShoppingCartControlPanel"/> class.
            /// </summary>
            /// <param name="autoGenerateViewCommands">if set to <c>true</c> [auto generate view commands].</param>
            public ShoppingCartControlPanel(bool autoGenerateViewCommands)
                : base(autoGenerateViewCommands)
            {
            }
            #endregion
     
            #region Properties
     
            /// <summary>
            /// Gets an instance of ShoppingCartManager.
            /// </summary>
            public virtual ShoppingCartManager Manager
            {
                get
                {
                    if (this.manager == null)
                        this.manager = new ShoppingCartManager(this.ProviderName);
                    return this.manager;
                }
            }
     
            /// <summary>
            /// The title of the view.
            /// </summary>
            /// <value></value>
            public override string Title
            {
                get
                {
                    return Messages.ModuleTitle;
                }
                set
                {
                    base.Title = value;
                }
            }
     
            #endregion
     
            #region WebControl Methods
     
            /// <summary>
            /// Restores control-state information from a previous page request that was saved by the System.Web.UI.Control.SaveControlState() method.
            /// </summary>
            /// <param name="savedState">An System.Object that represents the control state to be restored.</param>
            protected override void LoadControlState(object savedState)
            {
                if (savedState != null)
                {
                    object[] state = (object[])savedState;
                    base.LoadControlState(state[0]);
                }
            }
     
            /// <summary>
            /// Saves any server control state changes that have occurred since the time
            /// the page was posted back to the server.
            /// </summary>
            /// <returns>
            /// Returns the server control's current state. If there is no state associated
            /// with the control, this method returns null.
            /// </returns>
            protected override object SaveControlState()
            {
                return new object[] { 
                    base.SaveControlState()
                };
            }
     
            #endregion
     
            #region Control Panel Methods
     
            /// <summary>
            /// Loads configured views.
            /// </summary>
            protected override void CreateViews()
            {
                AddView<AdminView>("Administrator", "Administrator", "Administrator", null, null);
            }
     
            /// <summary>
            /// Returns a list of custom Command Panels.
            /// </summary>
            /// <param name="viewMode">Specifies the current View Mode</param>
            /// <returns>A list of command panels containing the defined commands for the current View Mode.</returns>
            protected override void CreateCustomCommandPanels(string viewMode, List<ICommandPanel> list)
            {
                list.Add(new ShoppingCartCommandPanel(this));
            }
     
            /// <summary>
            /// Returns the name of the default data provider for the module.
            /// Default data provider is used when no current provider is selected.
            /// The default implementation returns empty string.
            /// </summary>
            /// <returns>A string representing the name of the default data provider for the module.</returns>
            protected override string GetDefaultProviderName()
            {
                return ConfigurationHelper.Handler.DefaultProvider;
            }
     
            /// <summary>
            /// Refreshes the control hierarchy of the control
            /// </summary>
            /// <remarks>
            /// Recreates the controls' child controls.
            /// </remarks>
            public override void Refresh()
            {
                base.Refresh();
                this.manager = null;
            }
     
            #endregion
     
            #region Private Fields
     
            private ShoppingCartManager manager;
     
            #endregion
        }
    }



    ShoppingCartCommandPanel.cs
    using System;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Web.UI;
    using Telerik.Framework.Web;
    using Telerik.Web;
    using Telerik.Web.UI;
     
    using Moduleplex.ShoppingCart.Interfaces;
    using Moduleplex.ShoppingCart.Data;
    using Moduleplex.ShoppingCart.WebControls.Admin.Administrator;

    namespace Moduleplex.ShoppingCart.WebControls.Admin
    {
        /// <summary>
        /// Represents ShoppingCart module command panel. It is used to manage Products, Shipping, Store
        /// navigation of the admin part of ShoppingCart module
        /// </summary>
        public class ShoppingCartCommandPanel : ViewModeControl<ShoppingCartControlPanel>, ICommandPanel
        {
            #region Constructor
     
            /// <summary>
            /// Initializes a new instance of the <see cref="ShoppingCartCommandPanel"/> class.
            /// </summary>
            /// <param name="ctrlPnl">The CTRL PNL.</param>
            public ShoppingCartCommandPanel(IControlPanel ctrlPnl)
            {
                controlPanel = ctrlPnl;
            }
     
            #endregion
     
            #region Properties
     
            /// <summary>
            /// Gets or sets the path to a custom layout template for the control.
            /// </summary>
            [WebSysTemplate(layoutTemplateName,"CommandPanelLayoutTemplate","/ShoppingCart/",false,"2010-05-15")]
                
            public override string LayoutTemplatePath
            {
                get
                {
                    return base.LayoutTemplatePath;
                }
                set
                {
                    base.LayoutTemplatePath = value;
                }
            }
     
            /// <summary>
            /// Gets the name of the embedded layout template. This property must be overridden to provide the path (key) to an embedded resource file.
            /// </summary>
     
            protected override string LayoutTemplateName
            {
                get
                {
                    return layoutTemplateName;
                }
            }
     
            protected Object GetView(string viewName)
            {
                Type type = this.GetType();
                FieldInfo fieldInfo = type.GetField(viewName);
                return fieldInfo.GetValue(this);
     
            }
             
            /// <summary>
            /// Gets or sets the host of the command panel.
            /// </summary>
            public override ShoppingCartControlPanel Host
            {
                get
                {
                    if (host == null && controlPanel is ShoppingCartControlPanel)
                        host = (ShoppingCartControlPanel)controlPanel;
                    return host;
                }
                set
                {
                    host = value;
                }
            }
     
            /// <summary>
            /// Gets an instance of Manager.
            /// </summary>
            protected ShoppingCartManager Manager
            {
                get
                {
                    return Host.Manager;
                }
            }
     
            #endregion
     
            #region Control references
     
            /// <summary>
            /// Gets the function PanelBar.
            /// </summary>
            /// <value>The function PanelBar.</value>
            protected virtual RadPanelBar RpbFunction
            {
                get
                {
                    return base.Container.GetControl<RadPanelBar>("rpbFunction", true);
                }
            }
     
            /// <summary>
            /// Gets the Shopping Cart CSS.
            /// </summary>
            /// <value>The Shopping Cart CSS.</value>
            protected virtual CssFileLink ShoppingCartCss
            {
                get
                {
                    return base.Container.GetControl<CssFileLink>("shoppingCartCss", true);
                }
            }
     
            #endregion
     
            #region Methods
     
             /// <summary>
            /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
            }
     
            /// <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(Control viewContainer)
            {
                base.InitializeControls(viewContainer);
                BuildFunction();
            }
     
            private void BuildFunction()
            {
                IList<Function> functionData = new List<Function>();
                IList<Function> readAllFunction = this.Manager.ReadAllFunction();
                foreach (IFunction iFunction in readAllFunction)
                {
                    functionData.Add(new Function(iFunction.Id, iFunction.ParentId, iFunction.Name, iFunction.Description, iFunction.Perms, iFunction.Publish, iFunction.ViewName));
                }
                this.RpbFunction.DataTextField = "Name";
                this.RpbFunction.DataFieldID = "Id";
                this.RpbFunction.DataFieldParentID = "ParentId";
                this.RpbFunction.DataValueField = "ViewName";
                this.RpbFunction.ExpandMode = PanelBarExpandMode.SingleExpandedItem;
                this.RpbFunction.Width = Unit.Pixel(200);
                this.RpbFunction.EnableEmbeddedSkins = true;
                this.RpbFunction.Skin = "Sitefinity";
                this.RpbFunction.DataSource = functionData;
                this.RpbFunction.DataBind();
                this.RpbFunction.ItemClick += new RadPanelBarEventHandler(RpbFunction_ItemClick);
            }
            #endregion
     
            #region Event Handlers
     
            private void RpbFunction_ItemClick(object sender, RadPanelBarEventArgs e)
            {
                if (e.Item.Owner is RadPanelItem)
                {
                    RadPanelItem parentItem = (RadPanelItem)e.Item.Owner;
                    if (parentItem.Text == "Admin")
                    {
                        NavigateHostCommand<AdminView>(e.Item.Value);
                    }
                }
            }
            #endregion
     
            #region ICommandPanel Members
     
            /// <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="ICommandPanel">ICommandPanel</see> interface for more
            ///     complicated example implementing the whole
            ///     <see cref="ICommandPanel">ICommandPanel</see> interface.
            /// </example>
            public IControlPanel ControlPanel
            {
                get
                {
                    return controlPanel;
                }
            }
     
            /// <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="ICommandPanel">ICommandPanel</see> interface for more
            ///     complicated example implementing the whole
            ///     <see cref="ICommandPanel">ICommandPanel</see> interface.
            /// </example>
            public void Refresh()
            {
                this.ChildControlsCreated = false;
            }
     
            #endregion
     
            #region Private fields
     
            private IControlPanel controlPanel;
            private ShoppingCartControlPanel host;
     
            #endregion
     
            #region Constants
            /// <summary>
            /// The name of the embedded layout template.
            /// </summary>
            public const string layoutTemplateName = "Moduleplex.ShoppingCart.Resources.ControlTemplates.Backend.CommandPanel.ascx";
             
            #endregion
        }
    }

    AdminView.cs
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;
    using Telerik.Cms.Web.UI;
     
    namespace Moduleplex.ShoppingCart.WebControls.Admin.Administrator
    {
        class AdminView : ViewModeControl<ShoppingCartControlPanel>
        {
            protected override void CreateViews()
            {
                AddView<ConfigurationView<AdminView>>();
            }
     
            protected override void InitializeControls(Control viewContainer)
            {
                base.InitializeControls(viewContainer);
                string param = this.Page.Request.QueryString["Param"];
                if (param == "ConfigurationView")
                {
                    NavigateHostCommand<ConfigurationView<AdminView>>();
                }
            }
             
        }
    }

    ConfigurationView.cs
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Telerik.Cms.Web.UI;
    using Moduleplex.ShoppingCart;
     
    namespace Moduleplex.ShoppingCart.WebControls.Admin.Administrator
    {
        class ConfigurationView<THost> : ViewModeControl<THost> where THost : AdminView
        {
            #region Properties
     
            /// <summary>
            /// Gets or sets the path to a custom layout template for the control.
            /// </summary>
            [WebSysTemplate(layoutTemplateName, "ConfigurationViewLayoutTemplate", "/ShoppingCart/", false, "2010-05-15")]
     
            public override string LayoutTemplatePath
            {
                get
                {
                    return base.LayoutTemplatePath;
                }
                set
                {
                    base.LayoutTemplatePath = value;
                }
            }
             
     
            /// <summary>
            /// Gets the name of the embedded layout template.
            /// </summary>
            protected override string LayoutTemplateName
            {
                get
                {
                    return ConfigurationView<THost>.layoutTemplateName;
                }
            }
     
            #endregion
     
            #region Constants
     
            /// <summary>
            /// Specifies the embeded template name.
            /// </summary>
            protected const string layoutTemplateName = "Moduleplex.ShoppingCart.Resources.ControlTemplates.Backend.Administrator.ConfigurationView.ascx";
     
            #endregion
        }
    }

    Error Message
    Server Error in '/ShoppingCart' Application.
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
    Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
     
    Source Error:
     
    Line 195:                if (parentItem.Text == "Admin")
    Line 196:                {
    Line 197:                    NavigateHostCommand<AdminView>(e.Item.Value);
    Line 198:                }

     
     
    Source File: D:\My Programming\ASP.NET\CMS\Sitefinity\Sample Project\Sitefinity_Pluggable_Module\WebControls\Admin\ShoppingCartCommandPanel.cs    Line: 197
     
    Stack Trace:
     
    [ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index]
       System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) +64
       System.ThrowHelper.ThrowArgumentOutOfRangeException() +15
       System.Collections.Generic.List`1.set_Item(Int32 index, T value) +7458716
       Telerik.Cms.Web.UI.ViewModeControl`1.CreateHostViewCommand(String viewMode, String param, String parentId) +114
       Telerik.Cms.Web.UI.ViewModeControl`1.NavigateHostCommand(String param, String parentId, String resultMessage) +60
       Telerik.Cms.Web.UI.ViewModeControl`1.NavigateHostCommand(String param, String resultMessage) +108
       Telerik.Cms.Web.UI.ViewModeControl`1.NavigateHostCommand(String param) +109
       Moduleplex.ShoppingCart.WebControls.Admin.ShoppingCartCommandPanel.RpbFunction_ItemClick(Object sender, RadPanelBarEventArgs e) in D:\My Programming\ASP.NET\CMS\Sitefinity\Sample Project\Sitefinity_Pluggable_Module\WebControls\Admin\ShoppingCartCommandPanel.cs:197
       Telerik.Web.UI.RadPanelBar.RaiseEvent(Object eventKey, RadPanelBarEventArgs e) +75
       Telerik.Web.UI.RadPanelBar.OnItemClick(RadPanelBarEventArgs e) +11
       Telerik.Web.UI.RadPanelBar.RaiseItemClick(ControlItem item) +71
       Telerik.Web.UI.RadPanelBar.RaisePostBackEvent(String itemIndex) +42
       Telerik.Web.UI.RadPanelBar.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String itemIndex) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
     
     
    Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

    Thanks in advance
    Febry Nastrioman
  4. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    31 May 2010
    Link to this post
    Dear Telerik Team

    Please Advice

    Thanks
    Febry Nastrioman
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Jun 2010
    Link to this post
    Hello CafeDelMar,

    You are trying to navigate to a non existing view when you call

     NavigateHostCommand<AdminView>(e.Item.Value);

    So the parameter you are passing   (e.Item.Value); is not correct. It should return "Administrator" as implemented

    protected override void CreateViews()
            {
                AddView<AdminView>("Administrator", "Administrator", "Administrator", null, null);
            }


    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
Register for webinar
5 posts, 1 answered