Click here to Skip to main content
15,894,410 members
Articles / Web Development / HTML

ASP.NET HTML Editor Control

Rate me:
Please Sign up or sign in to vote.
4.91/5 (68 votes)
30 May 2013CPOL13 min read 188.7K   11.2K   130  
Creating a useful ASP.NET HTML Editor custom control.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using SigmaToolBox.TextEditor;
using Microsoft.Security.Application;

namespace SigmaToolBox
{

    namespace TextEditor
    {
        [ToolboxData("<{0}:RichTextBox runat=server></{0}:RichTextBox>")]
        public class RichTextBox : WebControl
        {
            /// <summary>
            /// This function sets a value to the text editor like some text or html data from database or other resources for edition activities
            /// </summary>
            /// <param name="Value"></param>
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public void SetValue(string Value)
            {
                string script = "disableElement(document.getElementById('textToolsContainer'));;document.getElementById('textEditor').style.display='none';isOnSourceMode=true;isOndesignMode=false;document.getElementById('sourceTxt').style.display='block';document.getElementById('sourceTxt').value='" + Value + "'";
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "valueSetterScript", script, true);
            }
            
            /// <summary>
            /// This property can be seted as True or false for get high level secured html code from editor
            /// </summary>
            public bool SetHighLevelSecurityForHtmlTags
            {
                set
                {
                    Security._SetHighLevelSecurityForHtmlTags = value;
                }
            }
            /// <summary>
            /// This function gets the data which has been provided in text editor for some purposes such as : demonstration in a specified page,storing in database,etc.
            /// </summary>
            /// <returns>string values</returns>
            public string GetValue()
            {
                return this.Page.Server.HtmlDecode(AntiXss.HtmlAttributeEncode(GetDecodedValue()));
            }

            /// <summary>
            /// Gets the Decoded value of a value which is encoded because of some security reasons
            /// </summary>
            /// <returns></returns>
            public string GetDecodedValue()
            {
                SourceActions Source = new SourceActions();
                Source.SourceProvider(this.Page);
                return this.Page.Server.HtmlDecode(Source._SourceCode);
            }
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                Page.Form.Enctype = "multipart/form-data";
            }
            
            protected override void OnPreRender(EventArgs e)
            {
                base.OnInit(e);
                Registrar.RegisterScripts(new List<string> { "SigmaToolBox.js.loading.js", "SigmaToolBox.js.testJS.js", "SigmaToolBox.js.Encoder.js", "SigmaToolBox.js.slider.js" }, this.Page, this.GetType());
                string InitializerJS = Page.ClientScript.GetWebResourceUrl(this.GetType(), "SigmaToolBox.js.Initializer.js");
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "RichText", "<script language=\"javascript\" src='" + InitializerJS + "'></script>");
            }
            
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                string Title = "<title>Kuleh.com</title>";
                string MetaTags = "<meta name=\"Description\" content=\"ایران گردی,گردشگری,کوله دات کام,kuleh.com,بهترین سایت گردشگری,نقشه راهنما,رستوران ها,هتل ها,نقشه راه ها,مسافرت,راهنمای سفر,دیدنی های ایران,سفر,راهنمای گردشگری,اطلاعات گردشگری\" /><meta http-equiv=\"Content-Type\" content=\"text/html\" /><meta name=\"google-site-verification\" content=\"_W5pX9ziDgLnVpTXszWQ2yWC7QiHeOGfA7CPtMAxqSg\" />";
                string Csslink = "<link rel='stylesheet' type='text/css' href='" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "SigmaToolBox.EditorStyles.Style.css") + "' />";
                string RichTextStyle = "<link rel='stylesheet' type='text/css' href='" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "SigmaToolBox.EditorStyles.textEditor.css") + "' />";
                string SliderStyle = "<link rel='stylesheet' type='text/css' href='" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "SigmaToolBox.EditorStyles.slider.css") + "' />";
                this.Page.Header.InnerHtml = Title + MetaTags + Csslink + RichTextStyle + SliderStyle;
            }
            
            protected override void RenderContents(HtmlTextWriter output)
            {
                if (this.Page.IsPostBack)
                {
                    //if there is an uploaded file
                    HttpFileCollection UploadFile =this.Page.Request.Files;
                    for (int i = 0; i < UploadFile.Count; i++)
                    {
                        HttpPostedFile file =   UploadFile[i];
                        string FileName = Path.GetFileName(file.FileName);
                        string StrPath = this.Page.MapPath("~/Uploads/");
                        try
                        {
                            file.SaveAs(Path.Combine(StrPath, FileName));
                        }
                        catch (DirectoryNotFoundException DirectoryNullException)
                        {
                            output.Write(DirectoryNullException.Message);
                        }
                    }
                }
                Page.ClientScript.GetPostBackEventReference(this.Page, string.Empty);
                HtmlSourceInitializer.InitializeHtmlSource(this.Page);
                output.Write(HtmlSourceInitializer.RichTextHtmlSource.ToString());
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student UFMG
Brazil Brazil
I am a PhD candidate in computer science. I started software development since 2006 and it seems like now it consumes most every part of my life. My life is my computer and solving problems, more specifically computational-mathematical problems, is my passion.I mostly enjoy areas like Machine Learning, Big Data and Brain-Computer Interface (BCI). My major goal in my life is becoming a person who utilizes everything he knows to improve human life quality. I also like hiking and reading self-development books.

Comments and Discussions