Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q2 2011 released 07/12/2011
select

Editor / OnClientPasteHtml

   
  
 
 
   

  • OnClientPasteHtml

    The OnClientPasteHtml event is useful in scenarios where the developers need to examine or modify the HTML to be pasted by an editor tool before it is inserted in the editor content area. Some common cases where the event can be used are:

    • Check whether user specified alt attribute for an image
    • Make modifications to a table being inserted (e.g. set a specific classname, etc)
    • Examine or modify a link before it is inserted
    • Modify the pasted content before its insertion in the content area
    The event allows the developer to cancel the event as well - then no content will be inserted. Many of the editor tools and dialogs use the pasteHtml method to perform their action - here is a complete list:

    Tools
    ---
    FormatStripper (when selection exists)
    InsertTable
    InsertTab (in IE)
    InsertSnippet
    InsertFormElement
    InsertGroupbox
    InsertDate
    InsertTime
    InsertSymbol
    InsertHorizontalRule
    InsertCustomLink
    Paste


    Dialogs
    ---------
    ImageManager
    FlashManager
    MediaManager
    TemplateManager
    TableWizard
    SetCellProperties
    SetImageProperties
    FormatCodeBlock
    RealFontSize

    Example:
    The code below demonstrates how to check whether the inserted image through the Image manager has an "alt" attribute set and if it doesn't then urge the user to enter an "alt" attribute name:

    <script type="text/javascript">
    function OnClientPasteHtml(sender, args)
    {
    var commandName = args.get_commandName();
    var value =
    args.get_value();

      if
    (commandName == "ImageManager")
      {
          //See if an img has an alt tag set
          var div = document.createElement("DIV");
         
          //Do not use div.innerHTML as in IE this would cause the image's src or the link's href to be converted to absolute path.
          //This is a severe IE quirk.
          Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div,value);
         
          //Now check if there is alt attribute
          var img = div.firstChild;
          if
    (!img.alt)
         {
            var alt = prompt("No alt tag specified. Please specify an alt attribute for the image", "");
            img.setAttribute("alt", alt);
           
            //Set new content to be pasted into the editor
            args.set_value(div.innerHTML);
         }
      }
    }
    </script>
    <telerik:RadEditor runat="server"
    OnClientPasteHtml
    ="OnClientPasteHtml"
    ImageManager-ViewPaths
    ="~/"
    ID
    ="RadEditor1">
    </telerik:RadEditor>

    The OnClientPasteHtml event is also fired when the Paste (Ctrl+V) command is executed. It is useful in scenarios when the pasted content should be modified and inserted in the content area.

    For example when copying and pasting a link or an image with a relative path in Internet Explorer, the browser automatically converts the path to absolute. The code below demonstrates how to attach to the OnClientPasteHtml event and strip the desired url path using the StripPathFilter content filter. The StripPathsFilter() method receives as a parameter an array of strings (devided by a white space) that will be stripped from the absolute path.

    <script type="text/javascript">
    function OnClientPasteHtml(sender, args)
    {
       
    var commandName = args.get_commandName();
        var value =
    args.get_value();
        
        if
    (commandName == "Paste")
        {
           
    // The StripPathsFilter() method receives as a parameter an array of strings (devided by a white space) that will be stripped from the absolute links.
           
    var domainName = "http://" + window.location.host; //returns the hostname and port number of the current URL
           
    var filter = new Telerik.Web.UI.Editor.StripPathsFilter([domainName]); //strip the domain name from the absolute path
                        
           
    var contentElement = document.createElement("SPAN");
           
    contentElement.innerHTML = value;
            var
    newElement = filter.getHtmlContent(contentElement);
            alert
    (newElement.outerHTML);
           
    args.set_value(newElement.outerHTML);  //set the modified pasted content in the editor
       
    }
    }
    </script>

    Please, note that the OnClientPasteHtml event fires the Paste command only when the StripFormattingOnPaste property is not set to "NoneSupressCleanMessage". In this case the editor does not process the pasted content and pastes it without modifications.

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Theme="Default" Language="vb" AutoEventWireup="true" CodeFile="DefaultVB.aspx.vb"
        Inherits="Telerik.Web.Examples.Editor.OnClientPasteHtml.DefaultVB" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
    <%@ Register TagPrefix="qsf" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <qsf:HeadTag ID="Headtag1" runat="server" />
    </head>
    <body class="BODY">
        <form id="form1" runat="server">
            <qsf:Header ID="Header1" runat="server" NavigationLanguage="VB" />
            <telerik:RadScriptManager ID="ScriptManager1" runat="server" />

            <script type="text/javascript">
            //<![CDATA[
            function OnClientPasteHtml(sender, args)
            {
                var commandName = args.get_commandName();
                var value = args.get_value();
                
                if (commandName == "InsertDate" || commandName == "InsertTime")
                {
                    //Set the inserted date / time string in a DIV with formatting
                    var div = document.createElement("DIV");
                    
                    Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div,value);
                    
                    //Set date/time information in a DIV element with applied color and background formatting
                    args.set_value("<div style='margin: 1px; padding:2px; width:auto; display:inline;border:solid 2px black; color:black; background-color:#FFCC00;'>" + div.innerHTML + "</div>");
                }
                    
                if (commandName == "ImageManager")
                {
                    //See if an img has an alt tag set
                    var div = document.createElement("DIV");
                    
                    //Do not use div.innerHTML as in IE this would cause the image's src or the link's href to be converted to absolute path.
                    //This is a severe IE quirk.
                    Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div,value);
                    
                    //Now check if there is alt attribute
                    var img = div.firstChild;
                    if (!img.alt)
                    {
                        var alt = prompt("No alt tag specified. Please specify an alt attribute for the image", "");
                        img.setAttribute("alt", alt);
                                        
                        //Set new content to be pasted into the editor
                        args.set_value(div.innerHTML);
                    }
                }
                
                if (commandName == "DocumentManager")
                {
                    //Set target="_blank" to the links inserted through the Document manager
                    var div = document.createElement("DIV");
                    
                    Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div,value);
                    //Now check if there is target attribute
                    var link = div.firstChild;
                    if (!link.target)
                    {
                        alert("No target attribute specified. The editor will automatically set target='blank' to the link");
                        link.setAttribute("target", "_blank");
                                        
                        //Set new content to be pasted into the editor
                        args.set_value(div.innerHTML);
                    }
                }
                
                if (commandName == "InsertTable")
                {
                    //Set border to the inserted table elements
                    var div = document.createElement("DIV");
                    
                    //Remove extra spaces from begining and end of the tag
                    value = value.trim();
                   
                    Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div,value);
                    var table = div.firstChild;
                   
                    if (!table.style.border)
                    {
                        table.style.border = "dashed 1px red";
                        //Set new content to be pasted into the editor
                        args.set_value(div.innerHTML);
                    }
                }

               //Cancel the event if you want to prevent pasteHtml to execute
               /*
                  args.set_cancel(true);
               */
            }
            //]]>
            </script>

            <telerik:RadEditor runat="server"
                OnClientPasteHtml="OnClientPasteHtml"
                ID="RadEditor1">
                <Tools>
                    <telerik:EditorToolGroup Tag="FileManagers">
                        <telerik:EditorTool Name="InsertTime" />
                        <telerik:EditorTool Name="InsertDate" />
                        <telerik:EditorTool Name="InsertTable" />
                        <telerik:EditorTool Name="DocumentManager" />
                        <telerik:EditorTool Name="ImageManager" />
                    </telerik:EditorToolGroup>
         </Tools>         
         <ImageManager ViewPaths="~/Editor/Img/UserDir/Marketing,~/Editor/Img/UserDir/PublicRelations" />
         <FlashManager ViewPaths="~/Editor/Img/UserDir/Marketing,~/Editor/Img/UserDir/PublicRelations" />
         <MediaManager ViewPaths="~/Editor/Img/UserDir/Marketing,~/Editor/Img/UserDir/PublicRelations" />
         <DocumentManager ViewPaths="~/Editor/Img/UserDir/Marketing,~/Editor/Img/UserDir/PublicRelations" />
         <TemplateManager ViewPaths="~/Editor/Img/UserDir/Marketing,~/Editor/Img/UserDir/PublicRelations" />
                 <Content>
                 
                 
         <img alt="product logo" src="../../Img/productLogoLight.gif" />is the successor of the well known industry standard Editor for ASP.NET. The tight integration with ASP.NET AJAX and the powerful new capabilities make Telerik's WYSIWYG Editor a flexible and lightweight component, turning it into the fastest loading Web Editor. Among the hottest features are:
         <ul>
                        <li><em>Single-file, drag-and-drop deployment</em></li>
                        <li><em>Built on top of ASP.NET AJAX</em></li>
                        <li><em>Unmatched loading speed with new semantic rendering </em></li>
                        <li><em>Full keyboard accessibility</em></li>
                        <li><em>Flexible Skinning mechanism</em></li>
                        <li><em>Simplified and intuitive toolbar configuration</em></li>
                        <li><em>Out-of-the-box XHTML-enabled output</em></li>
                        </ul>
         </Content>
            </telerik:RadEditor>
            <qsf:Footer runat="server" ID="Footer1" />
        </form>
    </body>
    </html>

Get more than expected!

 
 

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2024 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451