Try Now
More in this section
Blogs RSS feed

Replacing a child view with another one

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

[This article requires Sitefinity 3.6 hotfix to be installed]


We have demonstrated in the previous two articles how one can add a new child view to a View and then how one can remove the child view from a View. In this article, we will not learn anything new. All we are going to do is combine the two approaches in order to replace an existing child View with another one.


In order to replace a child view with another child view you will need to complete following steps:

  • Locate the host control which contains the child view you wish to replace
  • Remove the view you wish to replace
  • Add new view that should replace the old one we’ve removed in previous step
  • Make sure that when you are adding new View, the name of the View is exactly the same as the one that you have removed - by doing so you will ensure that Views have been replaced and not merely that one view was removed and another one added.

Example: Replacing NewsItemEdit View with a custom implementation of the same View

In this sample we are going to complete following task:


Every time a News Item is being edited (and saved) we will check if the title of the News Item contains word “dirty”. If so, we will redirect user to the website teaching people manners. This is obviously, rather absurd implementation; however it demonstrates a lot when it comes to the capabilities of the new backend architecture.


The following steps will outline how to complete our sample task:

  • First thing we need to do is implement our own NewsItemEdit View. Since we want to make only a small change to the built-in View we are going to derive a View from that one and override SaveContent method.
  • Add new C# class to your App_Code folder and name it CustomNewsItemEditView.cs
  • Replace the contents of the file with following code:
    using Telerik.News.WebControls.Admin; 
    using System.Web; 
    public class CustomNewsItemEditView : NewsItemEdit 
        public override System.Type LocalizationAssemblyInfo 
                return typeof(NewsItemEdit); 
                base.LocalizationAssemblyInfo = value; 
        public override System.Type AssemblyInfo 
                return typeof(NewsItemEdit); 
                base.AssemblyInfo = value; 
        protected override void SaveContent() 
            // check the title for word dirty with ridiculously flawed approached 
  • Qucik note here regarding the LocalizationAssemblyInfo and AssemblyInfo properties. The reason we are overriding them and setting them is because we wish to use embedded template and localization resources located in the Telerik.News assembly and since our View resides in App_Code assembly, it is necessary to set these properties manually. The SaveContent override itself is self explanatory, so we will not explain it in any more detail.
  • Now that we have our custom View, we will return to Controls Config file and perform the replacement. To do so, we will paste following declaration between the viewMap nodes.
    <viewSettings hostType="Telerik.News.WebControls.Admin.NewsItemsView, Telerik.News"
            <remove name="NewsItemEdit"></remove> 
            <add name="NewsItemEdit" viewType="CustomNewsItemEditView, App_Code"></add> 
Notice that we are following the rule for replacement, namely we are removing the “NewsItemEdit” View and then adding “NewsItemEdit” View, but the type (implementation) of the View we are adding is different than the one we have removed.


*** IMPORTANT *** 


Every time you modify ControlsConfig file it is necessary to restart the application in order for the changes to be applied. While there are numerous ways to restart an application, here are few handy ones - you can restart the IIS server, resave web.config file (e.g. open web.config file, press space, press backspace, save config.file), resave global.asax file (e.g. open global.asax file, press space, press backspace, save global.asax file)… 




With this we have finished our task and you can run the website to give it a try. This sample is also the last article in the topic regarding Controls Config file.

1 comment

Leave a comment
  1. Romi Mar 09, 2009
    I have remplaced CategoriesView with one custom categories. After generate my template and customclass I have used this method to remove standard class and use my class placed in App_Code with the class for template. The class as well removed and new class linked, but generate issue from the class of Template: NewControlPanel not defined. To work around, I have not placed my template class in App_Code folder as preconized but as codebehind, only the customized class, that inherit my template in App_Code.
    Could you add some complementary extended classes to test more this way?, Many time extended my templates i have frameworkinitialize issues. Regards.

    Leave a comment