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

Reference: Most important base classes - ViewModeControl class

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

 

ViewModeControl is the central class of the new Sitefinity backend architecture introduced in Sitefinity 3.6. Every time we create a View, we are basically creating a new class which inherits from the ViewModeControl (unless we are creating Views from User Controls).

 

In this article we will go over the ViewModeControl class, its usage and members.

 

Usage


ViewModeControl class is never used directly, but rather as a base class for creating Views. Typically, we would create a View as follows:
public class BlogsListView : ViewModeControl<BlogsView> 
From the example above it is obvious that we need to specify the generic parameter for the ViewModeControl which specifies the View which is the host of the current View. We have also discussed an alternate approach if we are to design flexible and ambivalent Views.

 

Once we have declared the class in such a way, we have declared the View and everything else that we do is an actual implementation of the View.

 


Properties


ViewModeControl exposes following public properties:
  • string Name
    Defines the name of the View. If this property is not set, the name of the View will be Type name (e.g. if the name of the class representing the View is BlogsListView, then the name of the View will be BlogsListView).
  • string Title
    Title of the View. This property is used with automatic command generation and automatic breadcrumb. So, for example if the Title of our View is “Blogs list”, the View will be represented in the breadcrumb as “Blogs list”. Also the automatically generated command link will have the same title as the View title.
  • string Description
    Description of the View. Used by automatic command generation to provide the description of the View, located right under the View title.
  • string ViewMode
    Gets or sets the name of the current View. Current View is always the last View loaded in the hierarchy of the Views. This property can be used to perform various conditioning, such as the one in case of the Blogs module, where different CommandPanels are loaded depending on the ViewMode.
    switch (this.ViewMode) 
          case "BlogsView"
          case "BlogsPermissionsView"
                // load blog command panels 
                break
          default
                // load post command panels 
          break
     
  • Type ViewModeType
    Gets the name of the Type of the current View. The property is very similar to ViewMode, with the exception that this property returns the type of the current View, while the ViewMode returns the name of the current View.
  • string DefaultViewMode
    Gets the default child View. The default child (or sub) View is the first View added to the collection of child views through the CreateViews method. When the View itself has no user interface (so called container View), Sitefinity will load the default View and present it to the user.
  • string ViewModeKey
    Gets the key of the View. This property has been abandoned in Sitefinity 3.6 Service Pack 1 in favor of routes and should not be used by developers.
  • string ParameterKey
    Gets the parameter query string key for straightforward retrieval of the optional parameter passed to the View. For example if View 1 passes a parameter to the View 2 in following manner:
    this.LinkToView2.NavigateUrl = this.CreateHostViewCommand<View2>(“someValue”); 
    View 2 can access this parameter in the following manner:
    string paramValue = this.Page.Request.QueryString[this.ParameterKey];
  • string ParentIdKey
    Gets the key of the parent id parameter, a secondary parameter that can be passed from one View to another. The ParamterKey and ParentIdKey function on the identical principle.
  • string AdditionalTemplateKey
    Returns the key for loading additional templates. Generally, this property is overridden if there is a need for it. Typically, this property is overridden to supply the name of the current provider. You can find more information on loading additional templates in this article.
  • IDictionary<Type, IViewInfo> Views
    Gets the dictionary of all the child views of the current View, with the View type as the key and ViewInfo as the value. This property is very useful in dynamic scenarios when Views are added or removed during the runtime, since it provides sufficient information on the current state of the hierarchy.
  • IViewInfo CurrentView
    Returns the view info (of IViewInfo type) on the current View.
  • THost Host
    Gets the reference to the host of the View. The type of this property is generic and it depends on the type with which View is initialized. This property is very useful for accessing the Views along the currently loaded hierarchy.
  • string LayoutTemplatePath
    Gets or sets the path to the external template to be used by this View. To learn more about different types of templates take a look at this article.
  • ITemplate LayoutTemplate
    Gets or sets the template to be used by this View. To learn more about different types of templates take a look at this article.
  • GenericContainer Container
    Views will always instantiate the template (regardless of how was the template declared) inside of the Container property. The Container property is of type GenericContainer, which provides simple and fast way for accessing controls inside of the template through it’s GetControl function, like it is demonstrated here:
    RadGrid dataGrid = base.Container.GetControl<RadGrid>("dataGrid"true);  
       
  • string LayoutTemplateName
    Gets the name of the embedded template to be used by the View. To learn more about different types of templates take a look at this article.
  • List<string> Route
    Gets the route to the current View. Route is a string list of ordered View names, starting with the root View and ending with the current View. Route can be used for dynamical working with the Views, however, most of the times working with the Host will give better and simpler access to the View hierarchy.
  • Type AssemblyInfo
    Gets or sets the assembly info on the assembly from which the embedded resources should be loaded. The property is of type Type and in order to set it, it is enough to set it to any Type which is located in the assembly with the embedded templates.
  • Type LocalizationAssemblyInfo
    Similar to the AssemblyInfo, LocalizationAssemblyInfo gets or sets info on the assembly in which localization messages are embedded. The property is of type Type and in order to set it, it is enough to set it to any Type which is located in the assembly with the embedded localization resources.
  • Guid DataItemId
    This is a helper property which will return the empty Guid if no parameter was passed to the View or if the passed parameter cannot be converted to a Guid. If on other hand, a parameter which can be converted to a Guid was passed to the View, this property will convert this parameter and return the Guid value passed as a parameter.
  • Guid ParentId
    This is a helper property which will return the empty Guid if no parent id parameter was passed to the View or if the passed parent id parameter cannot be converted to a Guid. If on other hand, a parent id parameter which can be converted to a Guid was passed to the View, this property will convert this parent id parameter and return the Guid value passed as a parent id parameter.

Methods


  • void InitializeControls(Control viewContainer)
    This method is typically overridden by the View which inherits from the ViewModeControl and this is where the View controls should be initialized. Pay attention that you do all your initializations here and not in CreateChildControls as it was practice in versions prior to Sitefinity 3.6. The viewContainer parameter passed to the method is the instance of the container object in which the child controls should be added. Here is an example of the implementation of this method:
    protected override void InitializeControls(Control viewContainer) 
                if (!this.Page.IsPostBack) 
                { 
                    this.ReportForm.Visible = true
                    this.ReportCreatedPanel.Visible = false
                } 
                this.SaveReport.Click += this.SaveReport_Click; 
                this.BackToReportsList.NavigateUrl = this.GetListCommand(); 
     
  • string CreateViewCommand<TView>()
  • string CreateViewCommand(string viewMode)
  • string CreateHostViewCommand<TView>()
  • string CreateHostViewCommand<TView>(string param)
  • string CreateHostViewCommand<TView>(string param, string parentId)
  • string CreateHostViewCommand(string viewMode, string param, string parentId)
  • string CreateRootViewCommand(params Type[] viewsChain)
  • string CreateRootViewCommand(string param, params Type[] viewsChain)
  • string CreateRootViewCommand(string param, string parentId, params Type[] viewsChain)
  • string CreateParameterCommand(string paramValue)
    Various overloads of the methods which create commands for navigating among Views. For more information on the usage of these methods, please consult following articles.
  • void NavigateHostCommand<TView>()•    void NavigateHostCommand<TView>(string param)
  • void NavigateHostCommand<TView>(string param, string resultMessage)
  • void NavigateHostCommand<TView>(string param, string parentId, string resultMessage)
  • void NavigateRootViewCommand(params Type[] viewsChain)
  • void NavigateRootViewCommand(string param, params Type[] viewsChain)
  • void NavigateRootViewCommand(string param, string parentId, params Type[] viewsChain)
    Various overloads of methods which will navigate from one View to another when called. For more information on the usage of these methods, please consult following article.
  • virtual void CreateViews()
    This method should be overloaded when one needs to add child views to the View. The method will be called the most opportune time by the base ViewModeControl. Here is an example of this method’s implementation:
    protected override void CreateViews() 
                AddView<BlogsView>(null"AllBlogs""AllBlogsDescription""all", Messages.ResourceManager); 
                AddView<BlogsPermissionsView>(null"Permissions""PermissionsDescription""globalPerm", Messages.ResourceManager); 
                AddView<PostsView>(); 
                AddView<BlogCommentsView>(); 
                AddView<BlogCategoriesView>(); 
                AddView<BlogTagsView>(); 
                AddView<BlogSettingsView>(); 
  • void SetDefaultView(string viewName)
    By default, the first View added to the collection of child Views will be the default child view. This method, however, allows this behavior to be changed. By calling this method and setting the name of the child view, we can specify the default child View of the View.
  • void AddView(string viewName, string virtualPath, string title, string description, string viewCommandCssClass, ResourceManager resources)
  • void AddView<TView>(string viewName, string title, string description, string viewCommandCssClass, ResourceManager resources) where TView : Control, new()
  • void AddView(Type type, string viewName, string title, string description, string viewCommandCssClass, ResourceManager resources)
  • void AddView<TView>() where TView : Control, new()
    Various overloads of methods for adding new child Views to the View. These methods are usually used inside of the CreateViews method. More on the usage of these methods can be found in this article.
  • void RemoveView<TView>()
  • void RemoveView(string viewName)
    Opposite from the AddView types of methods, these two overloads are used to remove the child View from the collection of the child Views. View can be removed either by its name or its type.

1 comment

Leave a comment
  1. Andy May 15, 2009
    Am I correct in saying that If I change the Name for a View [in CreateViews>AddView() in a Parent View] then navigation to the View using NavigateRootViewCommand doesn't work any more? (as that uses the type for the view, rather than the (changed) Name for the View?

    Leave a comment