You are not logged in.

#1 2007-08-09 21:15:29

gius
Member
Registered: 2007-08-09
Posts: 3

TinyMCE in ASP.NET

I couldn't find any implementation of TinyMCE for ASP.NET so I have spent a few minutes and written one of my own. I know it might not (and probably is not) be the best solution but I hope it could start a useful discussion over this problem.

To use TinyMCE in your ASP.NET website:
1.Install TinyMCE (copy the tinymce folder to your website root folder)
2.Create a C# Class file with this content:

Code:

namespace MyWebSite
{
    public class TextEditor : TextBox
    {
        protected override void OnPreRender(EventArgs e)
        {
            string tinyMceIncludeKey = "TinyMCEInclude";
            string tinyMceIncludeScript = "<script type=\"text/javascript\" src=\"/tinymce/jscripts/tiny_mce/tiny_mce.js\"></script>";

            if (!Page.ClientScript.IsStartupScriptRegistered(tinyMceIncludeKey))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), tinyMceIncludeKey, tinyMceIncludeScript);
            }

            if (!Page.ClientScript.IsStartupScriptRegistered(GetInitKey()))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), GetInitKey(), GetInitScript());
            }

            if (!CssClass.Contains(GetEditorClass())) //probably this is not the best way how to add the css class but I do not know any beter way
            {
                if (CssClass.Length > 0)
                {
                    CssClass += " ";
                }
                CssClass += GetEditorClass();
            }
            base.OnPreRender(e);
        }

        private string GetInitKey()
        {
            string simpleKey = "TinyMCESimple";
            string fullKey = "TinyMCEFull";

            switch (Mode)
            {
                case TextEditorMode.Simple:
                    return simpleKey;
                case TextEditorMode.Full:
                    return fullKey;
                default:
                    goto case TextEditorMode.Simple;
            }
        }

        private string GetEditorClass()
        {
            return GetEditorClass(Mode);
        }

        private string GetEditorClass(TextEditorMode mode)
        {
            string simpleClass = "SimpleTextEditor";
            string fullClass = "FullTextEditor";
            
            switch (mode)
            {
                case TextEditorMode.Simple:
                    return simpleClass;
                case TextEditorMode.Full:
                    return fullClass;
                default:
                    goto case TextEditorMode.Simple;
            }
        }

        private string GetInitScript()
        {
            string simpleScript = 
                "<script language=\"javascript\" type=\"text/javascript\">tinyMCE.init({{mode : \"textareas\",theme : \"simple\",editor_selector : \"{0}\"}});</script>";
            string fullScript = 
                "<script language=\"javascript\" type=\"text/javascript\">tinyMCE.init({{mode : \"textareas\",theme : \"advanced\",editor_selector : \"{0}\"}});</script>";

            switch (Mode)
            {
                case TextEditorMode.Simple:
                    return string.Format(simpleScript, GetEditorClass(TextEditorMode.Simple));
                case TextEditorMode.Full:
                    return string.Format(fullScript, GetEditorClass(TextEditorMode.Full));
                default:
                    goto case TextEditorMode.Simple;
            }
        }

        public override TextBoxMode TextMode
        {
            get
            {
                return TextBoxMode.MultiLine;
            }
        }

        public TextEditorMode Mode
        {
            get
            {
                Object obj = ViewState["Mode"];
                if (obj == null)
                {
                    return TextEditorMode.Simple;
                }
                return (TextEditorMode)obj;
            }
            set
            {
                ViewState["Mode"] = value;
            }
        }

        public enum TextEditorMode
        {
            Simple,
            Full
        }
    }
}

3.To use the TextEditor in your page, register the control in the page:

Code:

<%@ Register TagPrefix="cc" Namespace="MyWebSite" %>

and use this tag:

Code:

<cc:TextEditor runat="server" ID="txt1" Mode="Full" />
or
<cc:TextEditor runat="server" ID="txt2" Width="600px" Mode="simple">Hello World!</cc:TextEditor>

Take it as a starter kit, you can add more TextEditorModes or edit current configuration.

Please, let me know if you use or improve this code or about your opinion.

Last edited by gius (2007-08-14 12:06:18)

Offline

 

#2 2007-08-15 18:30:05

choreson
Member
Registered: 2007-08-10
Posts: 4

Re: TinyMCE in ASP.NET

Gius:

This is pretty cool!  I am using it in my application.  Really neat.  Thank you very much for sharing this wonderful trick!

Last edited by choreson (2007-08-15 18:30:30)

Offline

 

#3 2008-06-03 10:34:26

Kostkac
Member
Registered: 2008-06-03
Posts: 4

Re: TinyMCE in ASP.NET

Hello,

I have big problem. I would like to encapsulate TinyMCE just like this hint, but i have this error:

Unknown server tag 'cc:TextEditor'.

But when I try to create TextEditor in code behind, it’s ok - but it’s not comfortable. I can add control to place holder by Placeholder.Controls.Add(TextEditor)

Any ideas?

Last edited by Kostkac (2008-06-03 10:42:53)

Offline

 

#4 2009-04-07 20:54:43

pnr
Member
Registered: 2009-04-07
Posts: 1

Re: TinyMCE in ASP.NET

I am alsow getting the error: Unknown server tag 'cc:TextEditor'.

Have anybody found a souloution?

Offline

 

#5 2009-08-19 02:52:31

RE3Rotor
Member
Registered: 2009-08-19
Posts: 2

Re: TinyMCE in ASP.NET

Did you add the registration code?

Code:

<%@ Register TagPrefix="cc" Namespace="MyWebSite" %>

Offline

 

#6 2009-08-19 03:04:37

RE3Rotor
Member
Registered: 2009-08-19
Posts: 2

Re: TinyMCE in ASP.NET

This worked for me. Thank you so much.

Offline

 

#7 2009-08-25 19:16:41

rsp
Member
Registered: 2009-08-25
Posts: 1

Re: TinyMCE in ASP.NET

Im working with masterpages and have the reg-code (<%@ Register TagPrefix="cc" Namespace="MyWebSite" %>) on both my page.aspx where I want to use tinymce and tried on the masterpage.

My class-file i named tiny.cs where errors occure.

Get the error: (line:10) The name 'Page' does not exist in the current context.
(line:20)And same error on CssClass.

(line 28) Also get a error on: 'object' does not contain a defanition for on 'PreRender'

Any help appriciated!

Offline

 

#8 2009-09-09 15:11:39

owiZo
Member
Registered: 2009-09-09
Posts: 1

Re: TinyMCE in ASP.NET

pnr wrote:

I am alsow getting the error: Unknown server tag 'cc:TextEditor'.

Have anybody found a souloution?

For those who couldnt handle this,
create a new Web Service solution (in my case i created one on VS2008),
create a new C# Class file,
paste the code above,
publish it,
find the generated DLL
copy to your website folder,
add reference,
add tag:
<%@ Register TagPrefix="cc" Namespace="MyWebSite" Assembly="YourDLLFileName" %>
and you're good to go...

hope this helps.

Last edited by owiZo (2009-09-09 15:14:46)

Offline

 

#9 2010-01-02 21:38:08

Felix Riesterer
Administrator
From: Germany
Registered: 2005-12-30
Posts: 3901
Website

Re: TinyMCE in ASP.NET

@kole_manas

Learn to ask in the right place. This thread is about some totally different problem!


Greetings from Germany,

Felix Riesterer.
(-> about me and this forum <-)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2008 PunBB