Search code examples
javascriptc#htmlpre

Retrieve Code or PRE tag content on server side


I am trying to read the content of "PRE" or "CODE" tag of HTML to server side in C#. I am using HTML editor "tinymce". Preview is displayed properly. But when I read it on server side in string variable and the display it in some div. Then all the formatting is lost and code is shown in single line.

<pre>
  <code>
    function Panel(element, canClose, closeHandler) {
        this.element = element;
        this.canClose = canClose;
        this.closeHandler = function() {
            if (closeHandler) closeHandler()
        };
    }

  </code>

</pre>

Solution

  • You need to use the <pre>...</pre> tags to denote preformatted text, simply embedding in <div>...</div> tags will ignore all newlines unless you use <br /> to force a new line break

    For example, to embed the string in pre tags, you may use:

    string s = tbxTinymce.Text;
    s = "<pre>" + s + "</pre>";
    divOutput.InnerHtml = s;