Articles → ASP .NET AJAX → Customize Ajax HTML Editor In C#
Customize Ajax HTML Editor In C#
Why Customization Of AJAX HTML Editor Is Required?
Objective Of This Tutorial
The HTML Editor
Click to Enlarge
Prerequisites
- Download AjaxControlToolKit (version 3.5 and above).
- Copy the downloaded AjaxControlToolKit.dll in the bin folder of the project.
- Copy AjaxMin.dll to bin folder as well.
- Install visual studio 2008 (or above) on your machine.
- How to create a web based project in visual studio?
- What is a class?
- How to inherit a class?
- How to override a method?
- What are asp.net AJAX controls?
- How to register a control in an aspx page?
- How to add a web form in web based application in visual studio?
Customization Of The Editor
- Add bold, italic and underline buttons in the top tool bar
- Add HTML view and design view buttons in bottom tool bar
Solution
- Create a new website (or a web based project).
- Right click on the project and click on ‘Add New item’ option.
- Select "Code file" and set the name as "CustomEditor.cs".
- A class is created as shown in code below.
public class CustomEditor {}
- Include following name spaces in the class.
using AjaxControlToolkit.HTMLEditor;
using AjaxControlToolkit.HTMLEditor.ToolbarButton;
- In order to customize the toolbar buttons, you will have to derive a new HTML Editor from the base Editor class so that you can access the necessary functions required for customization of HTML Editor
public class CustomEditor : Editor{ }
- The two important methods required for customization of HTML Editor are FillTopToolbar() and FillBottomToolbar() method.
protected override void FillTopToolbar(){}
protected override void FillBottomToolbar(){}
- In order to accomplish our first goal, we will have to add buttons for bold, italic and underline by adding the following lines of code in the FillTopToolbar() function
protected override void FillTopToolbar() {
TopToolbar.Buttons.Add(new Bold());
TopToolbar.Buttons.Add(new Italic());
TopToolbar.Buttons.Add(new Underline());
}
- Inside the FillBottomToolbar() we will write the code to add buttons for HTML view and design view. So the method will be
protected override void FillBottomToolbar() {
BottomToolbar.Buttons.Add(new DesignMode());
BottomToolbar.Buttons.Add(new HtmlMode());
}
using AjaxControlToolkit.HTMLEditor;
using AjaxControlToolkit.HTMLEditor.ToolbarButton;
namespace MyControls {
public class CustomEditor: Editor {
protected override void FillTopToolbar() {
TopToolbar.Buttons.Add(new Bold());
TopToolbar.Buttons.Add(new Italic());
TopToolbar.Buttons.Add(new Underline());
}
protected override void FillBottomToolbar() {
BottomToolbar.Buttons.Add(new DesignMode());
BottomToolbar.Buttons.Add(new HtmlMode());
}
}
}
How To Use This Control In Aspx Page
- Use @Register directive to register the required user defined control on a web form. This directive forms a connection between a tag prefix and a custom control. The following line of code creates an association between "MyControls" and "HTMLEditor". Add the following tag for registering the control.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
- Add a tool kit script manager tag on the page. This is very important. ToolKitScriptManager handles all JavaScript scripts for partial page rendering to update your server controls without refreshing the page. It is mandatory for this control to be present in order to make ajax controls work.
<ajax:ToolkitScriptManager ID="ScriptManager1" runat="server">
</ajax:ToolkitScriptManager>
- You can now add the editor control as follows
<HTMLEditor:CustomEditor ID="editor" runat="server" />
Click to Enlarge