nasr nasr - 19 days ago 6
C# Question

How can i set tiny_mce content with c sharp

I have a textarea tiny_mce such as

<textarea id="Textarea1" name="abc" rows="15" cols="80" style="width: 80%">

</textarea>


I want to set it in page load.
If i add runat server , this change to "pure textarea" that is not tiny_mce control.
How can i set content from c sharp?

Answer

Keep in mind that TinyMCE is a JavaScript-based editor that runs 100% in the client - there are no server side APIs to talk to TinyMCE.

The API to load content is a JavaScript API that runs in the browser:

https://www.tinymce.com/docs/api/tinymce/tinymce.editor/#setcontent

If you want to use this API you need to pass the data from the server to the browser and then call setContent() with that data. You can throw the data into a JavaScript variable, make an AJAX call to fetch the data as the page loads - you have several choices.

If you want to do this server side the only option is to place the HTML inside the tag. Something like:

<textarea>
    &lt;p&gt;text &amp;lt; text&lt;/p&gt;
</textarea> 

A major complication with this approach is that the HTML you place in the <textarea> needs to be escaped as in my example above. This is not a requirement when using the setContent() API.

UPDATE: If refreshing sometimes make this work you likely have a timing issue - you are likely trying to use TinyMCE APIs before the editor is initialized. If your goal is to load content when TinyMCE is loaded you can use something like this in the TinyMCE configuration:

setup: function (editor) {
  editor.on('init', function () {
      // Your AJAX call to get the content
      this.setContent(variableWithTheContent);
  });
}

This function will not get called before the editor is initialized so you can be sure that APIs like setContent() will work at that point.