Click here to Skip to main content
15,668,126 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 2 Mar 2011

Stats

41.9K views
4 bookmarked

Get TinyMCE Value from Server-Side in ASP.NET 4.0

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
3 Mar 2011CPOL1 min read
Using TinyMCE is fairly simple, but attempting to access the value entered by the user in ASP.NET 4.0 presents with some problems.
First, download TinyMCE and unzip it. Add the "tiny_mce" folder to a new ASP.NET 4.0 website project. Add a new ASPX page to your project, and add the following code to that page (VB.NET version shown first):
ASP.NET
<%@ Page Language="vb" ValidateRequest="false" %>

<script runat="server">
    Protected Sub GetValue_Click(ByVal sender As Object, ByVal e As EventArgs)
        lblDisplay.Text = txtMain.Text
    End Sub
</script>

<html>
<head>
    <title>TinyMCE in ASP.net 4.0</title>
    <script type="text/javascript" src="/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript">
        tinyMCE.init({
            mode : "textareas"
        });
    </script>
</head>
<body>
    <form id="frmMain" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtMain" TextMode="MultiLine" Rows="20" Columns="100">
            This is your initial text.
        </asp:TextBox>
        <br />
        <asp:Button runat="server" Text="Get Value" onclick="GetValue_Click" />
        <br />
        <asp:Label runat="server" ID="lblDisplay" />
    </div>
    </form>
</body>
</html>

Here's the C# version:
ASP.NET
<%@ Page Language="C#" ValidateRequest="false" %>

<script runat="server">
    protected void GetValue_Click(object sender, EventArgs e)
    {
        lblDisplay.Text = txtMain.Text;
    }
</script>

<html>
<head>
    <title>TinyMCE in ASP.net 4.0</title>
    <script type="text/javascript" src="/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript">
        tinyMCE.init({
            mode : "textareas"
        });
    </script>
</head>
<body>
    <form id="frmMain" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtMain" TextMode="MultiLine" Rows="20" Columns="100">
            This is your initial text.
        </asp:TextBox>
        <br />
        <asp:Button runat="server" Text="Get Value" onclick="GetValue_Click" />
        <br />
        <asp:Label runat="server" ID="lblDisplay" />
    </div>
    </form>
</body>
</html>


If you are using ASP.NET 2.0, you are done. However, in ASP.NET 4.0, you have to modify the web.config by adding requestValidationMode="2.0":
XML
<system.web>
    <httpRuntime requestValidationMode="2.0" />
</system.web>

If you run into the following error when you click the button on the webpage:
ASP.NET error message:
A potentially dangerous Request.Form value was detected from the client (txtMain="<p>This is your init...").

Make sure you have requestValidationMode="2.0" in the web.config and make sure you have ValidateRequest="false" at the top of your page.

Caution: Practically speaking, it is necessary to turn off request validation in order to submit data using the TinyMCE editor, but doing so comes with some caveats. Disabling request validation may make your site more vulnerable to script attacks, depending on how you handle the user input on pages with no request validation. Explaining those risks and the precautions to prevent malicious code is outside the scope of this tip/trick, but you can start to learn more about the issue here.

License

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


Written By
Web Developer
United States United States

  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
BugPlease check your code Pin
Member 197314525-May-14 1:20
Member 197314525-May-14 1:20 
QuestionRe: Please check your code Pin
AspDotNetDev25-May-14 9:31
protectorAspDotNetDev25-May-14 9:31 
Questionmy vote 5, One more thing I would like to add to this tip/trick Pin
Nitin S17-Apr-13 19:53
professionalNitin S17-Apr-13 19:53 
GeneralMy understanding is that adding {encoding: "xml"} to the tin... Pin
Richard Fawcett18-May-11 22:14
Richard Fawcett18-May-11 22:14 
GeneralRe: Interesting. I'll give it a try next chance I get and let yo... Pin
AspDotNetDev19-May-11 18:58
protectorAspDotNetDev19-May-11 18:58 
GeneralTesting comment creation and deletion... testing more. Pin
AspDotNetDev3-Mar-11 14:12
protectorAspDotNetDev3-Mar-11 14:12 
GeneralI found that all you need is the ValidateRequest="false" at ... Pin
Ed Nutting3-Mar-11 6:56
Ed Nutting3-Mar-11 6:56 
I found that all you need is the ValidateRequest="false" at the top of the page but then I'm not sure if the server I'm working on is running in 3.5 or 4.0, I suspect 3.5; So you may well be right I just don't see why they would suddenly make force putting stuff in the web config as well as on the page :/
GeneralRe: Indeed. 3.5 is just a layer built on top of .Net 2.0. 4.0 is... Pin
AspDotNetDev3-Mar-11 7:20
protectorAspDotNetDev3-Mar-11 7:20 
GeneralReason for my vote of 4 My 4 :) Good but you may want to exp... Pin
Ed Nutting2-Mar-11 20:19
Ed Nutting2-Mar-11 20:19 
GeneralRe: Setting request validation mode to 2.0 is the only way I've ... Pin
AspDotNetDev2-Mar-11 20:33
protectorAspDotNetDev2-Mar-11 20:33 
GeneralRe: FYI, I added a note about the dangers of disabling request v... Pin
AspDotNetDev3-Mar-11 14:03
protectorAspDotNetDev3-Mar-11 14:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.