Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Microsoft Content Management Server Development : A Placeholder Control to Store All HTML Tags (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/26/2013 6:06:35 PM

The AllTagsHtmlPlaceholderControl Class

Begin by adding a class file named AllTagsHtmlPlaceholderControl.cs to the UsefulPlaceholderControls project. Add the required namespaces above the namespace declaration:

using System;
using Microsoft.ContentManagement.Publishing;
using Microsoft.ContentManagement.Publishing.Extensions.Placeholders;
using Microsoft.ContentManagement.WebControls.Design;
using Microsoft.ContentManagement.WebControls;

namespace UsefulPlaceholderControls
{
  . . . code continues . . .
}

As we are simply modifying the way the HtmlPlaceholderControl behaves, the control will inherit from the HtmlPlaceholderControl class and support only HtmlPlaceholderDefinitions:

[SupportedPlaceholderDefinitionType(typeof(HtmlPlaceholderDefinition))]
						public class AllTagsHtmlPlaceholderControl : HtmlPlaceholderControl
{
  public AllTagsHtmlPlaceholderControl()
  {
  }
}

We don’t need to do anything in the constructor, so leave it empty.

Saving All Kinds of Tags

The trick behind getting the placeholder to accept all kinds of tags is to not save them as tags in the first place.

Tags are defined by an opening less-than sign (<), the element’s name, followed by a closing greater-than sign (>). For example, the tag that describes the start of a script block is <script>.

To fool the placeholder into accepting every tag, regardless of whether it’s a simple bold tag <b> or a script tag <script>, we will replace all less-than (<) and greater-than (>) characters with the strings “~LT~” and “~GT~” respectively. As a result, what gets saved to the placeholder is just funny looking text like this:

~LT~script~GT~
function Greet()
{
    alert(document.all.MyTextBox.value);
}
~LT~/script~GT~

The converted text does not contain any restricted tags. It is saved to the placeholder without any problems. Interestingly, the popular Telerik r.a.d. editor uses the same technique to allow special tags.

Since the conversion process is done when the posting is saved, we will modify the SavePlaceholderContent() method. Add the overridden SavePlaceholderContent() method directly below the constructor. Within the save, we will call a method named Escape() (defined right after this) that converts all tags to text.

protected override void SavePlaceholderContent(PlaceholderControlSaveEventArgs e)
{
  EnsureChildControls();
  ((HtmlPlaceholder)(this.BoundPlaceholder)).Html = Escape(this.Html);
}

					  

Escaping Tags

The Escape() method simply replaces all less-than (<) characters with ~LT~ and all greater-than characters (>) with ~GT~.

It is probably worth noting that only HTML markup tags will contain the < and > characters. If an author enters these characters as part of the main text within the placeholder, MCMS encodes the less-than character to &lt; and the greater-than character to &gt;. Therefore we don’t have to worry about the Escape() routine messing up content entered by the author.

private string Escape(string input)
{
  return input.Replace("<","~LT~").Replace(">","~GT~");
}

UnEscaping Tags

The act of reverting the ~LT~ and ~GT~ characters back to the < and > characters is done by the UnEscape() method. Add it directly below the Escape() method.

private string UnEscape(string input)
{
  return input.Replace("~LT~","<").Replace("~GT~",">");
}

Loading Content for Authoring and Presentation

To display the content, all we need to do is unescape the ~LT~ and ~GT~ strings back to the < and > characters respectively.

The first place where content is displayed is in authoring view when authors update the placeholder’s content. We only need to convert the text when the placeholder is being edited, or when the Web Author is in the AuthoringReedit mode. The UnEscape() method defined earlier does the conversion. Add the overridden LoadPlaceholderContentForAuthoring() method shown below:

protected override void LoadPlaceholderContentForAuthoring(
                                              PlaceholderControlEventArgs e)
{
  EnsureChildControls();
  if (WebAuthorContext.Current.Mode == WebAuthorContextMode.AuthoringReedit)
  {
    base.Html = UnEscape(((HtmlPlaceholder)(base.BoundPlaceholder)).Html);
  }
  else
  {
    base.LoadPlaceholderContentForAuthoring(e);
  }
}

					  

Uploaded content is, needless to say, also shown in presentation view. Regardless of whether or not the content is displayed when viewing the posting for preview or published modes, the ~LT~ and ~GT~ strings must be converted back to < and > characters respectively. We will do the conversion in the overridden LoadPlaceholderContentForPresentation() method. Append the method to the code:

protected override void LoadPlaceholderContentForPresentation(
                                              PlaceholderControlEventArgs e)
{
  EnsureChildControls();
  base.Html = UnEscape(((HtmlPlaceholder)(base.BoundPlaceholder)).Html);
}

					  

The control is complete. Save and build the solution.

Using the AllTagsHtmlPlaceholderControl

To use the AllTagsHtmlPlaceholderControl, copy the UsefulPlaceholderControl.dll library file to the bin directory or add it as a reference to the project. After adding the control to the Toolbox, drag and drop it onto template files as you would with regular placeholder controls. Set the PlaceholderToBind property of the control to an existing HtmlPlaceholderDefinition.

In addition, if authors are planning to save restricted tags like <script> blocks and <input> elements, you will need to set the AllowHtmlSourceEditing property of the control to true. The WYSIWYG editor does not allow authors to copy and paste the restricted tags in Design view. They will have to click on the HTML button on the authoring toolbar before pasting the HTML code into the control.

Other -----------------
- Sharepoint 2013 : Create a Team Site, Create an Enterprise Wiki Site in SharePoint Server, Create a Blog Site
- Sharepoint 2013 : Create a Subsite
- SQL server 2008 R2 : Reverting to a Database Snapshot for Recovery
- SQL server 2008 R2 : Setup and Breakdown of a Database Snapshot
- Windows Home Server 2011 : Maintaining Windows Home Server - Checking Free Disk Space on the System Drive
- Windows Home Server 2011 : Maintaining Windows Home Server - Checking Your Hard Disk for Errors
- Windows Home Server 2011 : Maintaining Windows Home Server - Checking System Uptime
- HP ProLiant Servers AIS : How Memory Works
- HP ProLiant Servers AIS : Memory and Cache
- SQL Server 2008 R2 : A Performance Monitoring Approach (part 3) - Monitoring Memory, Monitoring the Disk System
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server