This is a discussion on How to create an HTML Editor for ASP.NET AJAX within the C# Programming forums, part of the Software Development category; Introduction Most blog, forum and Wiki applications use an HTML editor as the primary authoring tool for site content. With ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Introduction Most blog, forum and Wiki applications use an HTML editor as the primary authoring tool for site content. With this type of control, an online user can create and edit an HTML document. The user is able to modify the text -- including its format, fonts and colors -- as well as add links and images. Often, they may also view and/or edit the HTML source.. Microsoft AJAX (ASP.NET AJAX Extensions) introduces a new implementation model for server controls with related script. This article discusses how to create an HTML editor server control specifically for the Microsoft AJAX environment. |
| Sponsored Links |
| |||
| Most modern browsers now support the "editing" of displayed HTML content by the user. When the document designMode property is set to true, the rendered HTML on the page can be edited by the user. While in designMode, the document's execCommand method supports additional commands that enable "programmatic" modification of document contents. For example, passing the command string "bold" as the first parameter to execCommand causes the selected text to appear bold by adding appropriate HTML tags and/or attributes. The resources listed at the end of this article discuss designMode and execCommand in more detail and describe how to implement a basic HTML editor. Also, the source code available with this article can be examined for implementation examples. |
| |||
| As part of ASP.NET AJAX, Microsoft introduced a new "model" for extending the capabilities of a server control with client-side script. That model is described in an ASP.NET AJAX tutorial that should be read along with this article. In general, we create two related controls: * A server control that implements the IScriptControl interface * A related client control derived from Sys.UI.Control (part of the client-side AJAX library) In order for ScriptManager to know how to create and initialize the related client control, we implement the new IScriptControl interface, adding two callback methods. Then we add a few lines of code to OnPreRender and Render to trigger those callbacks at appropriate times in the page life cycle. The specifics are described in the tutorial and again in the discussion of HtmlEditor.cs below. We also encapsulate our client-side behavior in a JavaScript class, implemented as a Microsoft AJAX client control. This permits the client control object to be created and initialized in a standard way by the client-side AJAX code. The specifics are described in the tutorial and again in the discussion of HtmlEditor.js below. |
| |||
| Component appearance: * 2 toolbar rows across the top * 2 tabs along the bottom * An editor area that displays and/or edits the document in either mode ![]() Last edited by Sundaram : 09-03-2007 at 08:15 AM. |
| |||
| hi sundaram Could you please tell me about Component Parts is HtmlEditor.js also with code thanks in advance M Ramesh Kumar Last edited by Mramesh : 09-05-2007 at 12:46 AM. |
| |||
| HtmlEditor.cs The component itself contains many different HTML elements, so the server control class is derived from CompositeControl. In addition, the class must implement the IScriptControl methods: Code: public class HtmlEditor : CompositeControl, IScriptControl Code: protected override void CreateChildControls()
{
...
CreateToolbars(...);
this.Controls.Add(CreateHtmlArea());
this.Controls.Add(CreateDesignArea());
this.Controls.Add(CreateTabbar());
this.Controls.Add(CreateUpdateArea());
base.CreateChildControls();
} Code: protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference htmlEditorReference =
new ScriptReference("Winthusiasm.HtmlEditor.HtmlEditor.js",
"Winthusiasm.HtmlEditor");
return new ScriptReference[] { htmlEditorReference };
} Code: protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor descriptor =
new ScriptControlDescriptor("Winthusiasm.HtmlEditor",
this.ClientID);
descriptor.AddProperty("htmlencodedTextID", this.HtmlEncodedTextID);
...
return new ScriptDescriptor[] { descriptor };
} Code: protected override void OnPreRender(EventArgs e)
{
...
if (!this.DesignMode)
{
// Test for ScriptManager and register if it exists.
sm = ScriptManager.GetCurrent(Page);
if (sm == null)
throw new HttpException(
"A ScriptManager control must exist on the current page.");
sm.RegisterScriptControl(this);
...
}
base.OnPreRender(e);
} Code: protected override void Render(HtmlTextWriter writer)
{
if (!this.DesignMode)
sm.RegisterScriptDescriptors(this);
base.Render(writer);
} |
| |||
| HtmlEditor.js Because the client-side behaviors for an HTML editor are fairly extensive, there is a fairly extensive amount of JavaScript required in the client control. Most of this is typical designMode client-side programming. More to the point of this article, the entire JavaScript code structure has been formatted to follow the client-side coding model recommended by Microsoft for all client controls built upon the AJAX client-side libraries. This includes: * Namespace registration * Constructor * Prototype block with all methods comma separated * Prototype get and set methods for each property passed from the server control * Prototype initialize method * Prototype dispose method * Descriptor method * Class registration Namespace registration: Code: Type.registerNamespace("Winthusiasm"); Code: Winthusiasm.HtmlEditor = function(element)
{
Winthusiasm.HtmlEditor.initializeBase(this, [element]);
this._htmlencodedTextID = "";
...
} Code: Winthusiasm.HtmlEditor.prototype =
{
method1: function()
{
...
},
method2: function()
{
...
},
...
} Code: get_htmlencodedTextID: function()
{
return this._htmlencodedTextID;
},
set_htmlencodedTextID: function(value)
{
this._htmlencodedTextID = value;
},
... Code: initialize: function()
{
Winthusiasm.HtmlEditor.callBaseMethod(this, 'initialize');
...
}, Code: dispose: function()
{
...
Winthusiasm.HtmlEditor.callBaseMethod(this, 'dispose');
}, Code: Winthusiasm.HtmlEditor.descriptor =
{
properties: [ {name: 'htmlencodedTextID', type: String },
... ]
} Code: Winthusiasm.HtmlEditor.registerClass("Winthusiasm.HtmlEditor",
Sys.UI.Control); |
| |||
| hi sundaram, Could you please tell me Use Model in HtmlEditor. and Thanks for Component Parts are HtmlEditor.cs and HtmlEditor.js thanks in advance M Ramesh Kumar Last edited by Mramesh : 09-05-2007 at 01:11 AM. |
| |||
| Using the Code It includes: * The Winthusiasm.HtmlEditor control project * A sample website that uses the HtmlEditor control * A solution that contains both Use Model * Create the website as an "ASP.NET AJAX-enabled Web Site" * Copy the assembly Winthusiasm.HtmlEditor.dll to the Bin folder * Add a Register statement to the page * Add a custom control Tag(s) to the page * Use the Text property to set the editor HTML * Save the HTML when appropriate * Use the Text property to get the saved HTML Example Register statement: Code: <%@ Register TagPrefix="cc"
Namespace="Winthusiasm.HtmlEditor"
Assembly="Winthusiasm.HtmlEditor" %> Code: <cc:HtmlEditor ID="Editor" runat="server" Height="400px" Width="600px" /> Code: Editor.Text = initialText; |
| |||
| Use Model Details: Saving the HTML when Appropriate A Save button (or similar functionality) will most likely be placed on the page with the editor. When that element is triggered, the editor's "client-side" Save method should be called. This instructs the editor to store the current HTML (converting to XHTML if appropriate) and clears the modified flag. The sample web page defines a GetHtmlEditor() JavaScript method that demonstrates how to "find" the client-side editor object using the Microsoft AJAX $find syntax: Code: function GetHtmlEditor()
{
return $find('<%= Editor.ClientID %>');
} Code: OnClientClick="GetHtmlEditor().Save()" Code: DataStore.StoreHtml(Editor.Text); |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to use ajax in asp.net and how to install ajax in my system | vel.m8 | ASP and ASP.NET Programming | 5 | 04-08-2008 08:55 PM |
| If I ask you to write 'VI' editor in C++ - How you'll proceed? | Sabari | C and C++ Programming | 0 | 07-23-2007 01:37 AM |
| How to create html from the content of the textbox? | Balasubramanian.S | C# Programming | 4 | 03-29-2007 08:30 AM |
| What Is Your Favorite HTML Editor? | spid4r | Web Design Help | 0 | 03-08-2007 10:39 PM |
| which is the best html editor ? | webmaster | HTML, CSS and Javascript Coding Techniques | 11 | 03-08-2007 12:40 PM |