0

I have a tinymce editor and I am wanting to grab the HTML contents of the editor within my C# ASPX code - but I am not entirely sure on the right way of doing this?

Can somebody please suggest a best practice?

I know I can get the HTML Content by calling this from javascript...but how would I pass that result back to my ASP.NET C# for storing in a database for example:

tinymce.activeEditor.getContent()
RenegadeAndy
  • 5,440
  • 18
  • 70
  • 130

2 Answers2

1

Assuming you've bound TinyMCE to a textarea with runat="server", then in C# you can access the HTML via the textarea's InnerHtml property.

sh1rts
  • 1,874
  • 1
  • 13
  • 14
1

Set your Page validate request to false first:

<%@ Page ValidateRequest="false" ..

then add an id and runat property to your textarea:

<textarea id="txtEditor" runat="server" ... ></textarea>

Let's say on a button click you want to grab the information like this:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string text1 = txtEditor.InnerHtml;  //includes HTMLs
        string text2 = txtEditor.InnerText;  //just plain text

        //store the value into the database here
    }

You could also add the first line into your web.config file if your are using .NET Framework 4 +

<system.web>
     <pages validateRequest="false" />
     <httpRuntime requestValidationMode="2.0" />
     ....

And if you do not want to have that globally you could point it to the page only using web.config as well:

Just add this one at the very end of your web.config file just before the </configuration>

 ....
 <location path="WebForm2.aspx"> <!-- add your page path here -->
  <system.web>
    <pages validateRequest="false" />
    <httpRuntime requestValidationMode="2.0" />
  </system.web>
 </location>
</configuration>
Ali
  • 2,574
  • 1
  • 17
  • 24
  • Well..not sure this will work either, because I am using tinymce - which generates the html when I call a specific javascript function. In this case - how will this work? – RenegadeAndy Jul 09 '14 at 02:10
  • When I do this - I get the following error:A potentially dangerous Request.Form value was detected from the client. This seems like its an unsafe solution - how can i verify non nasty html input from the user? – RenegadeAndy Jul 09 '14 at 02:14
  • You must set the `ValidateRequest="false"` to your page as I said at the first line. and it will work just fine afterwards. – Ali Jul 09 '14 at 02:16
  • That is already the case. I am using Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446 – RenegadeAndy Jul 09 '14 at 02:16
  • In terms of safety you could read more about this error here: http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client – Ali Jul 09 '14 at 02:18
  • Sorry the problem is still not solved Ali. I get the same error with ValidateRequest="false". I seem unable to add the requestValidationMode param to my webconfig as I get this error: The configuration section 'httpRuntime' cannot be read because it is missing a section declaration – RenegadeAndy Jul 09 '14 at 02:24
  • Setting *validateRequest* to false globally (in web.config) is probably not a good idea; it should only be done on a page-by-page basis i.e. his pages using TinyMCE only. – sh1rts Jul 09 '14 at 02:24
  • @RenegadeAndy it should be nested within system.web - is it ? – sh1rts Jul 09 '14 at 02:25
  • Aha - that was it! Thanks for your help. – RenegadeAndy Jul 09 '14 at 02:32