AJAX Tutorial Customizing HTML Editor Control Toolkit


V4 Ajax Tutorials
Server Intellect Cloud Hosting

AJAX Tutorial Customizing HTML Editor Control Toolkit

 

 

Using the HTML Editor in ASP.NET C# 

Were you looking for an AJAX tutorial or how to Learn AJAX and use the AJAX control kit HTML Editor Control?

In This Tutorial we are going to provide you with a brief but detailed overview of the HTML Editor that is packaged along with the AJAX Control Toolkit.  The HTML Editor is filled with plethora of options for changing font size , Selecting a font changing background colors adding links, changing the foreground, adding images text alignment and performing copy & paste functions.

 

First Things first let’s start by adding a ScriptManager control to the page, before we can even begin using the HTML Editor Tool Kit. The ScriptManager Control is found under the AJAX extensions Tab in Visual Studio Web Developer 2010.

After Adding the ScriptManager to the page, we will want to Add HTML Editor Control, in this case it is called Editor Control in the Toolkit.

If you're ever in the market for some great Windows web hosting, try Server Intellect.We have been very pleased with their services and most importantly, technical support.

Simply after dragging over the HTML editor over to the page, you can then set the properties in the properties toolbox. It is normal to set the Width and Height Properties to fit a page,  I have placed the code below that illustrates that we just did the steps above.

 

<%@ Page Language="C#" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

 

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        ltlResult.Text = Editor1.Content;

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

      

        <asp:ScriptManager ID="ScriptManager1" runat="server" />

       

        <cc1:Editor

            ID="Editor1"

            Width="450px" 

            Height="200px"

            runat="server"/>

        <br />

        <asp:Button

            id="btnSubmit"

            Text="Submit"

            Runat="server" onclick="btnSubmit_Click" />

   

        <hr />

        <h1>You Entered:</h1>

       

        <asp:Literal

            id="ltlResult"

            Runat="server" />

   

    </div>

    </form>

</body>

</html>

 

In the Page above it contains Button control and a literal control, when you click the button the contents of the HTML editor Appear in the Literal control.


The HTML editor content property is used to locate the html entered in the HTML editor. Be advised that this HTML content can contain JavaScript. In the next section we will talk on how to protect yourself from JavaScript Injections.

 

Let’s Customize the HTML Editor ToolBar

Did you want to control what buttons appear in the editor? Well you can; for example, you might want to remove the HTML TAB to prevent users from switching over to the HTML editor into HTML Mode, quite a mouthful huh?  This is used to prevent user from using some super large Fonts in a Forum to post an overly ridiculous Message in a post.

Below we have posted the Code Behind File code to override or prevent users controls

using AjaxControlToolkit.HTMLEditor;

 

namespace MyControls

{

    public class CustomEditor : Editor

    {

        protected override void FillTopToolbar()

        {

            TopToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.Bold());

            TopToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.Italic());

        }

 

        protected override void FillBottomToolbar()

        {

            BottomToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.DesignMode());

            BottomToolbar.Buttons.Add(new AjaxControlToolkit.HTMLEditor.ToolbarButton.PreviewMode());       

        }

    }

 

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).

<%@ Page Language="C#" %>

<%@ Register namespace="MyControls" tagprefix="custom" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Show Custom Editor</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    <asp:ScriptManager ID="ScriptManager1" runat="server" />

   

    <custom:CustomEditor

        Width="450px" 

        Height="200px"

        runat="server" />

   

    </div>

    </form>

</body>

</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.

Summary

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
45465468 said:

4456468

Posted Oct 25, 2010 1:37 PM
45465468 said:

asdf

Posted Oct 25, 2010 1:37 PM
45465468 said:

asdfd

Posted Oct 25, 2010 1:37 PM
45465468 said:

ewaefeaw

Posted Oct 25, 2010 1:38 PM
45465468 said:

you must use a captcha!!!!

Posted Oct 25, 2010 1:38 PM
45465468 said:

carajote

Posted Oct 25, 2010 1:38 PM
Ranjit said:

Please tell me how to put more fonts in editor controll

Posted Nov 28, 2010 3:23 PM
Mano said:

Good article. Excellent!!!

Posted Dec 1, 2010 7:10 PM
Freelance SEO India said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Dec 29, 2010 11:20 AM
Cem said:

You had better to add an image button for Editor =(

Posted Dec 30, 2010 5:00 AM
Muhammmad siddique said:

its nice info about html editor

Posted Jun 17, 2011 7:16 AM
santosh said:

nice info regarding custom editor, but how to fill font name and font size drop down. no items shown under these drop downs.

Posted Jul 1, 2011 7:40 AM
aasdsadsad said:

<html>

<head>

</head>

<body>

test....

</body>

</html>

Posted Aug 3, 2011 4:33 AM
www.outlet-louisvuitton.co.uk said:

www.outlet-louisvuitton.co.uk

Posted Aug 6, 2011 3:29 AM
Puneet said:

Excellent

Posted Aug 19, 2011 8:16 AM
ram said:

can anybody tell me how to find caret position in ajax editor

Posted Sep 13, 2011 12:36 AM
kiran said:

how to image button in this editor

Posted Oct 20, 2011 5:11 AM
Surya kant said:

good

Posted Oct 20, 2011 5:39 AM
Surya kant said:

Good article.How to show font size dropdown like 8px,10px....72px like that show in ms word

Posted Oct 20, 2011 5:41 AM
http://localhost:1095/Website/images/ajax-loader.gif said:
Posted Oct 28, 2011 11:04 AM
Liaouw said:

very useful~thx.

Posted Dec 21, 2011 9:45 PM
Air yeezys said:

, it can be difficult to determine a good starting point. Visual Web Developer has loads of AJAX controls available to us to help get the job done, but some may be overkill for simple AJAX page

Posted Jan 14, 2012 3:09 AM

Leave a Comment