Thursday, June 24, 2010

Microsoft Ajax Editor Control With ToolsFile

I come to ajxt toolkit editor control and find that there's no option or property like radeditor where we can define the path of ToolsFile to give dynamic look to toolbars.

I try to implement a extender control for Editor which having the property ToolsFile

Example:

Extender Control
------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjaxControlToolkit.HTMLEditor;
using System.ComponentModel;
using System.Web.UI;
using System.Xml.Linq;
using System.Web;
using System.Reflection;
using AjaxControlToolkit.HTMLEditor.ToolbarButton;
using System.Collections.ObjectModel;

namespace SazzControls
{
    public class editor : Editor
    {
        ///
        /// Created by Asif Ghanchi Dated On June 21 2010
        ///

        #region " Local Variable "
        #endregion
        #region " Properties"

        [Description("Relative path of Tools File"), Category("Appearance"), Bindable(BindableSupport.Yes),
        UrlProperty("*.xml")]
        public string ToolsFile
        {
            get
            {
                return (Convert.ToString(ViewState["ToolsFile"]) == null) ? string.Empty : Convert.ToString(ViewState["ToolsFile"]);
            }
            set
            {
                ViewState["ToolsFile"] = value;
            }
        }

        #endregion
        #region " Constructor "
        #endregion
        #region " Control Methods "

        protected override void FillTopToolbar()
        {
            if (ToolsFile != string.Empty)
            {
                XDocument xd = XDocument.Load(HttpContext.Current.Server.MapPath(ToolsFile));
                Assembly asm = Assembly.GetAssembly(typeof(AjaxControlToolkit.HTMLEditor.ToolbarButton.CommonButton));

                foreach (XElement tool in xd.Descendants("tool"))
                {
                    Type t = asm.GetTypes().FirstOrDefault(cb => cb.Namespace == "AjaxControlToolkit.HTMLEditor.ToolbarButton" && cb.Name == tool.FirstAttribute.Value);
                    if (t != null)
                    {
                        CommonButton _tool = (CommonButton)Activator.CreateInstance(t);
                        TopToolbar.Buttons.Add(_tool);
                        foreach (XElement option in tool.Descendants("option"))
                        {
                            Collection opts = null;
                            AjaxControlToolkit.HTMLEditor.ToolbarButton.SelectOption opt;
                            if (tool.FirstAttribute.Value == "FontName") opts = ((FontName)_tool).Options;
                            else if (tool.FirstAttribute.Value == "FontSize") opts = ((FontSize)_tool).Options;
                            opt = new AjaxControlToolkit.HTMLEditor.ToolbarButton.SelectOption();
                            opt.Value = option.Attribute("value").Value;
                            opt.Text = option.Attribute("text").Value;
                            opts.Add(opt);
                        }
                    }
                }
            }
        }

        #endregion
        #region " Methods "
        #endregion
        #region " Enum "
        #endregion
    }
}

---------------------------
XML File

download
--------------------------------

Please give comments for above if you having any query or suggestion regarding this.

No comments:

Post a Comment