Introduction
CKEditor is one of the most popular web text editors nowadays. Here is the official Site of CKEditor. Usually, we use this tool for easy and friendly back end data entry. This tool is rapidly used for composing email messages. On the other hand, CKEditor is an open source application, which means it can be modified any way we want. So I found it very useful for our day to day practice. Today, I am going to describe an easy way to integrate CKEditor with ASP.NET web projects.
Background
As a web developer, I found that I am using CKEditor on a regular basis. Each time, I was getting into some trouble that took my work hour unnecessarily. That's why I made a decision to find an easy way to integrate this useful tool with my ASP.NET web projects.
Points of Interest
I think this will be interesting and time consuming for CMS type data entry where CKEditor is quite often used. As this process can be used easily and it supports easy integration between different projects, programmers will be helped a lot.
Using the Code
Try the following easy steps to complete the task.
- Open New Project [File->New->Project-> ASP.NET Empty Web Application]
- Name the Project as you like, I named it "
TestCkEditor
"
- Download the attached Zip file (CKEDditor.zip) and extract.
- You will find a bin folder when you extract the CKEditor.zip
- Go to the Solution explorer
[References->Right
Mouse Click -> Add References->Browse-> Then browse the bin folder of the extracted (CkEditor.zip) -> Select The Two DLLs
(CKEditor.dll & CKEditor.Net.dll)->OK]
- There is a folder called ckeditor in small caps. Find the folder. This folder contains all necessary files (.js , .css, images, themes, etc. that are necessary for successful integration of this tool.
- Copy this folder and paste it in your project.
- Then add a new aspx page to the project.
- Then write this single line of code just before the HTML tag of your .aspx page. This block of line will register the DLL file that you have just added & using the "
TagPrefix
" will call the CKEditorControl
. Here the BasePath is the folder you have just added which contains all necessary files to load the CKEditor
.
<%@ Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
- Then add this
div
inside of the form
tag of your aspx page and we are done.
<div>
<CKEditor:CKEditorControl ID="CKEditor1" BasePath="/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
</div>
- Build the project and then run. You will find CKEditor with full features at your screen. Then customize as per your requirements.
Here is the back end code under button click.
HtmlEncode
and HtmlDecode
are useful ways to use in here. HTML encoding makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML. For example, if there is a string
in the CKEditor
which contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as the opening or closing bracket of an HTML tag. When the characters are HTML encoded, they are
converted to the string
s < and>, which causes the browser to display the less than sign and greater than sign correctly.
protected void Button1_Click(object sender, EventArgs e)
{
string str = CKEditor1.Text;
string str1 = Server.HtmlEncode(str);
string str2 = Server.HtmlDecode(str);
lblText.Text = str2;
}
On the other hand, Html Decoding does not convert any tag to string
. It simply interprets your input into plain HTML.
Here is an example:
There is a paragraph in the CKEditor
. So the HtmlDecode
converts into a plain HTML. On the other hand, HtmlEncode
converts them into string
.
So now I think you will find it easier to work with CKEditor
.
Happy coding.