Click here to Skip to main content
15,892,927 members
Articles / Web Development / ASP.NET

HTMLEditor Provider - How to write a custom provider for ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.75/5 (29 votes)
5 Sep 200614 min read 122.8K   2.4K   123  
A tutorial on how to use the Provider Templates to create your own provider.
using System;
using System.Configuration;
using System.Configuration.Provider;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SeaburyDesign;

namespace MyCompany1
{
    public class TextboxHTMLEditorProvider : HTMLEditorProvider
    {
        private TextBox textbox = new TextBox();

        public override System.Web.UI.Control HTMLEditor
        {
            get { return textbox; }
        }

        public override System.Web.UI.WebControls.Unit Width
        {
            get { return textbox.Width; }
            set { textbox.Width = value; }
        }

        public override System.Web.UI.WebControls.Unit Height
        {
            get { return textbox.Height; }
            set { textbox.Height = value; }
        }

        public override string Text
        {
            get { return textbox.Text; }
            set { textbox.Text = value; }
        }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if ((config == null) || (config.Count == 0))
                throw new ArgumentNullException("You must supply a valid configuration dictionary.");

            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "TextboxHTMLEditorProvider");
            }

            //Let ProviderBase perform the basic initialization
            base.Initialize(name, config);

            //Perform feature-specific provider initialization here
            //A great deal more error checking and handling should exist here

            textbox.TextMode = TextBoxMode.MultiLine;
            textbox.Wrap = true;

            string text = config["text"];
            if (!String.IsNullOrEmpty(text))
                Text = text;
            else
                Text = "";

            string height = config["height"];
            if (!String.IsNullOrEmpty(height))
                Height = Unit.Pixel(Convert.ToInt32(height));
            else
                Height = Unit.Pixel(600);

            string width = config["width"];
            if (!String.IsNullOrEmpty(width))
                Width = Unit.Pixel(Convert.ToInt32(width));
            else
                Width = Unit.Pixel(800);
            
            ////Get the connection string
            //string connectionStringName = config["connectionStringName"];
            //if (String.IsNullOrEmpty(connectionStringName))
            //    throw new ProviderException("You must specify a connectionStringName attribute.");

            //ConnectionStringsSection cs =
            //    (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");
            //if (cs == null)
            //    throw new ProviderException("An error occurred retrieving the connection strings section.");

            //if (cs.ConnectionStrings[connectionStringName] == null)
            //    throw new ProviderException("The connection string could not be found in the connection strings section.");
            //else
            //    connectionString = cs.ConnectionStrings[connectionStringName].ConnectionString;

            //if (String.IsNullOrEmpty(connectionString))
            //    throw new ProviderException("The connection string is invalid.");
            //config.Remove("connectionStringName");

            ////Check to see if unexpected attributes were set in configuration
            //if (config.Count > 0)
            //{
            //    string extraAttribute = config.GetKey(0);
            //    if (!String.IsNullOrEmpty(extraAttribute))
            //        throw new ProviderException("The following unrecognized attribute was found in " + Name + "'s configuration: '" +
            //                                    extraAttribute + "'");
            //    else
            //        throw new ProviderException("An unrecognized attribute was found in the provider's configuration.");
            //}
        }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer Virtual RadioLogic
United States United States
Todd Davis has been working in web and application development for several years, using Silverlight, ASP.NET, VB.NET, C#, C++ and Javascript, as well as a great deal of work with SQL server and IIS.

He currently works for Virtual Radiologic in Eden Prairie, MN, however he is better known for his varied work in the open source community, especially the DotNetNuke project for which he provided several world-renowned training videos and modules. A huge advocate of open source and open knowledge sharing, everything on his website (www.SeaburyDesign.com) is always offered for free.

Whenever he is not actively coding at his laptop (a rarity to be sure), he can be found woodworking, walking with his wife and kids, or motoring along the back roads of MN on his Harley Davidson Fatboy.

Comments and Discussions