|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
ASP.NET Designing a Rich Text Editor .NET Server Control
Designing a Rich Text Editor .NET Server Control
By: Chandu Thota
Dec. 16, 2002 12:00 AM
One of the biggest advantages of ASP.NET Server Controls is that you can package a lot of functionality into a Server Control and reuse it in the UI layer. The .NET Framework ships with a wide range of .NET Server Controls that come in handy for almost everything that you usually do in a typical Web implementation scenario. In this article we're going to build a text box Server Control that works like a Word document and can be used to format the text without knowing any HTML! This is very useful because this Server Control will simplify life when you need to publish well-formatted content! As you would imagine, this does a lot of things with client-side technologies such as JavaScript and DHTML, so I assume you have an in-depth knowledge of DHTML and .NET Server Controls. With this background, let's take a look at how to build a Rich Text Editor .NET Server Control that can be used to publish rich content. The language I'm using is VB.NET.
Rich Text Editor Design
Now the goal is clear. Before we jump into the VB.NET coding specifics for the Server Control, here is how we will achieve these design requirements.
1. Ability to Edit/Enter the Content
2. Ability to Format the Content
varIframe.document.execCommand Of course, you can also use CTRL + B to achieve the same result.
3. Ability to Extract the HTML from the Rich Content With this background, let's dig into some coding specifics.
Developing the Rich Text Editor Server Control
Public Class RTFEditor I'm going to discuss only the most important functions of this class, but you can download it from the support material (the class is named RTFEditor.VB) for a look at the full class (the source code for this article is available at www.sys-con.com/dotnet/sourcec.cfm). This class defines several Public Properties, such as Width, Height, and so on. But the properties that are important in this context are RichHTML and RichText. The RichHTML property is a ReadOnly Public Property that returns a string - the HTML string for the Rich Formatted Text entered. If you want to extract the HTML string from the rich content that you have entered, this is the property to use. It is defined below:
Public ReadOnly Property RichHtml() As String The RichText property is a Public Property that takes in an HTML string and sets the Rich Text Content to the control. If you want to set some Rich Text to the Server Control to display, this is the property that you should use:
Public Property RichText() As String Next, you should look at the CreateChildControls. We're going to override this method to provide the child controls that are required for the Rich Text Server Control, such as the buttons that can be used for formatting the text, the iFrame element, and the hidden input element to hold the HTML string. The method implementation is partially shown in Listing 1. The PrepareLinkControl method actually creates the image buttons and adds the "OnClick" attribute to call executeCommand to format the text. The next method we'll look at is PrepareBrowserMsgJs. This function checks the browser version to see whether the Rich Text Control will work. The browser details are obtained from the HTTP Request context as shown below:
Dim hbc As New Then we need to check for the version of the browser to see if this Rich Text control will work (see Listing 2) Finally, let's look at the method, LoadPostData, responsible for capturing the HTML that is copied from the iFrame element to the hidden field upon submitting the Web form. The LoadPostData method is one of the methods you have to implement when you implement the interface IpostBackDataHandler. The method's implementation is shown below:
Public Function LoadPostData(ByVal postDataKey As String, As you can see, the hidden field is accessed in the control to extract the HTML string. This completes the development of the Rich Text Editor Control. Now let's move to actually using this control in an ASPX page.
Using the Rich Text Editor Control
<%@ Register TagPrefix="eSynaps" Namespace="RTFEditor" And then add the actual control itself:
<eSynaps:RTFEditor id="myeditor" runat="server" height="300" To test this, let's add a button to post the form back and a <div> element to display the HTML for the formatted rich content.
<asp:Button ID="mybutton" Runat="server" Text="Get the HTML" Upon formatting some rich text content when you post back the Web Form, the Button Click event will set the HTML to the <div> elements innerHTML property, as shown below:
Private Sub mybutton_Click(ByVal sender As System.Object, _ Figure 2 shows how the Rich Content Editor works. You can see the HTML for the Rich Text that I have formatted in the Rich Text Control displayed in the bottom of the form.
Summary Happy .NET 'ing! Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||