ASP.NET (snapshot 2017) Microsoft documentation and samples

How do I use the HTML Editor Control? (C#)

by Microsoft

HTMLEditor is an ASP.NET AJAX Control that allows you to easily create and edit HTML content via buttons in a toolbar.

The goal of this tutorial is to provide you with an overview of the HTML Editor control included with the AJAX Control Toolkit. The HTML Editor includes options for changing font size, selecting a font, changing background color, modifying the foreground color, adding links, adding images, changing text alignment, and performing cut, copy, and paste operations (see Figure 1).

The HTML Editor

Figure 01: The HTML Editor(Click to view full-size image)

The HTML editor enables you to enter content using a design mode or you can enter HTML directly. You also are provided with the option to preview your HTML content (see Figure 2).

Design, HTML, and Preview buttons

Figure 02: Design, HTML, and Preview buttons(Click to view full-size image)

In this tutorial, you learn how to display the HTML Editor, how to customize the toolbar buttons that appear in the HTML Editor, and how to avoid Cross-Site Scripting Attacks.

Displaying the HTML Editor

Before you can use the HTML Editor in an ASP.NET page, you must first add a ScriptManager control to the page. The ScriptManager control is located beneath the AJAX Extensions tab in the Visual Studio/Visual Web Developer Express toolbox.

You should place the ScriptManager control at the top of the page before any other controls on the page. For example, you can place it immediately below the opening server-side <form> tag.

The HTML Editor control is located in the toolbox with the rest of the AJAX Control Toolkit controls. It is named the Editor control (see Figure 3).

The HTML Editor control

Figure 03: The HTML Editor control(Click to view full-size image)

After you drag the HTML Editor onto a page, you can set its properties in the property sheet. For example, you normally want to set the Width and Height properties. Listing 1 contains the source for an ASP.NET page that contains an HTML editor.

Listing 1 - SimpleEditor.aspx


   1:  <%@ Page Language="C#" %>
   2:  <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5:  <script runat="server">
   7:      protected void btnSubmit_Click(object sender, EventArgs e)
   8:      {
   9:          ltlResult.Text = Editor1.Content;
  10:      }
  11:  </script>
  13:  <html xmlns="http://www.w3.org/1999/xhtml">
  14:  <head id="Head1" runat="server">
  15:      <title></title>
  16:  </head>
  17:  <body>
  18:      <form id="form1" runat="server">
  19:      <div>
  21:          <asp:ScriptManager ID="ScriptManager1" runat="server" />
  23:          <cc1:Editor 
  24:              ID="Editor1" 
  25:              Width="450px"  
  26:              Height="200px"
  27:              runat="server"/>
  28:          <br />
  29:          <asp:Button
  30:              id="btnSubmit"
  31:              Text="Submit"
  32:              Runat="server" onclick="btnSubmit_Click" />
  34:          <hr />
  35:          <h1>You Entered:</h1>
  37:          <asp:Literal
  38:              id="ltlResult"
  39:              Runat="server" />
  41:      </div>
  42:      </form>
  43:  </body>
  44:  </html>

The page in Listing 1 contains an HTML Editor control, a Button control, and a Literal control. When you click the button, the contents of the HTML Editor appear in the Literal control (see Figure 4).

Submitting a form with an HTML Editor

Figure 04: Submitting a form with an HTML Editor(Click to view full-size image)

The HTML Editor Content property is used to retrieve the HTML content entered into the HTML Editor. Be aware that this HTML content can contain JavaScript. In the next section, we discuss how you can prevent JavaScript Injection Attacks.

Customizing the HTML Editor Toolbar

You can customize exactly which buttons appear in the editor. For example, you might want to remove the HTML tab to prevent users from switching the HTML Editor into HTML mode. Or, you might want to remove the font size dropdown list to prevent users from creating overly large text in a forum message post (see Figure 5).

A customized HTML Editor

Figure 05: A customized HTML Editor(Click to view full-size image)

You customize the toolbar buttons by deriving a new HTML Editor from the base Editor class. For example, the custom editor in Listing 2 only contains toolbar buttons for bold and italic. All other toolbar buttons have been removed. Furthermore, the HTML tab has been removed from the bottom of the editor (but the Design and Preview tabs are still there).

Listing 2 - App_Code.cs


   1:  using AjaxControlToolkit.HTMLEditor;
   3:  namespace MyControls
   4:  {
   5:      public class CustomEditor : Editor
   6:      {
   7:          protected override void FillTopToolbar()
   8:          {
   9:              TopToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.Bold());
  10:              TopToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.Italic());
  11:          }
  13:          protected override void FillBottomToolbar()
  14:          {
  15:              BottomToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.DesignMode());
  16:              BottomToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.PreviewMode());        
  17:          }
  18:      }
  19:  }

You must add the class in Listing 2 to your App_Code folder so that the class will be compiled automatically. If the App_Code folder does not exist in your website then you can simply add the folder.

After you create a custom editor, you can add it to an ASP.NET page in the same way as you add the normal HTML Editor (see Listing 3).

Listing 3 - ShowCustomEditor.aspx


   1:  <%@ Page Language="C#" %>
   2:  <%@ Register namespace="MyControls" tagprefix="custom" %>
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:  <html xmlns="http://www.w3.org/1999/xhtml">
   5:  <head id="Head1" runat="server">
   6:      <title>Show Custom Editor</title>
   7:  </head>
   8:  <body>
   9:      <form id="form1" runat="server">
  10:      <div>
  12:      <asp:ScriptManager ID="ScriptManager1" runat="server" />
  14:      <custom:CustomEditor 
  15:          Width="450px"  
  16:          Height="200px"
  17:          runat="server" />
  19:      </div>
  20:      </form>
  21:  </body>
  22:  </html>

Avoiding Cross-Site Scripting (XSS) Attacks

Whenever you accept input from a user, and redisplay that input on your website, you potentially open your website to Cross-Site Scripting (XSS) Attacks. In theory, a malicious hacker could submit JavaScript code that gets executed when the input is redisplayed. The JavaScript could be used to steal user passwords or other sensitive information.

Normally, you can defeat XSS attacks by HTML encoding whatever input you retrieve from a user before displaying it in a web page. However, HTML encoding the output of the HTML Editor would not only encode <script> tags, it would also encode all HTML tags. In other words, you would lose all of the formatting such as the font type, font size, and background color.

If you are collecting sensitive information from your users – such as passwords, credit-card numbers, and social security numbers - then you should not display un-encoded content that you retrieve from a user with the HTML Editor. You should use the HTML Editor only in situations in which you are not redisplaying the HTML content, or the HTML content is being submitted to your website by a trusted party.

Imagine, for example, that you are creating a blog application. In this situation, it makes sense to use the HTML Editor when composing blog posts. You are the only one who submits a blog post and, presumably, you can trust yourself not to submit malicious JavaScript. However, it does not make sense to use the HTML Editor when allowing anonymous users to post comments. You should be especially careful in situations in which users submit sensitive information such as passwords. Potentially, a malicious user could post a comment that contains the right JavaScript for stealing a password.


In this tutorial, you were provided with a brief overview of the HTML Editor control included in the AJAX Control Toolkit. You learned how to use the HTML Editor to accept rich content from a user and submit the content to the server. We also discussed how you can customize the toolbar buttons that are displayed by the HTML Editor. Finally, you learned how to avoid Cross-Site Scripting Attacks when using the HTML Editor to accept potentially malicious input.


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/htmleditor/how-do-i-use-the-html-editor-control-cs.htm