Replacing a child view with another one

Replacing a child view with another one

February 25, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

[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 
        { 
            get 
            { 
                return typeof(NewsItemEdit); 
            } 
            set 
            { 
                base.LocalizationAssemblyInfo = value; 
            } 
        } 
     
        public override System.Type AssemblyInfo 
        { 
            get 
            { 
                return typeof(NewsItemEdit); 
            } 
            set 
            { 
                base.AssemblyInfo = value; 
            } 
        } 
     
        protected override void SaveContent() 
        { 
            // check the title for word dirty with ridiculously flawed approached 
            if(base.ContentName.Text.ToLower().Contains("dirty")) 
                HttpContext.Current.Response.Redirect("http://www.wikihow.com/Be-Polite"); 
     
            base.SaveContent(); 
        } 
     
  • 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"
          <views> 
            <remove name="NewsItemEdit"></remove> 
            <add name="NewsItemEdit" viewType="CustomNewsItemEditView, App_Code"></add> 
          </views> 
    </viewSettings> 
     
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)… 

 

*** END IMPORTANT ***

 

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.
progress-logo

The Progress Guys

View all posts from The Progress Guys on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar