AJAX HTMLEditorExtender on postback tables don't display

2.7k views Asked by At

I am currently using an Ajax tool; HTMLEditorExtender to turn a textbox into a WYSIWYG editor, in a C# ASP.NET project. On the initial page load I place a large amount of formated text and tables into the editor which appears fine; even the tables.

The data is loaded into an asp:panel and the items/display from the panel is what is actually loaded into the extender and displayed.

However, if I want to have a button that saves all of the data that is in the editor to a Session and after the button press still display everything in the WYSIWG editor on the page postback everything that loads in the the textbox is fine except for the tables. They come up with the tags. Is there anyway around this?

The code I am using to initially load the page is this:

ContentPlaceHolder cphMain = (ContentPlaceHolder)this.Master.FindControl("MainContent");
Panel pnlContent = (Panel)cphMain.FindControl("innerFrame");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlContent.RenderControl(hw);
txtPN.Text = sb.ToString();
pnlContent.Visible = false;

On the button click I am having this saved:

string strHTMLText = txtPN.Text;
Session["ProgressNoteHTML"] = strHTMLText;

And I am loading it on the postback like this:

txtPN.Text = (string)Session["ProgressNoteHTML"];
ContentPlaceHolder cphMain = (ContentPlaceHolder)this.Master.FindControl("MainContent");
Panel pnlContent = (Panel)cphMain.FindControl("innerFrame");
pnlContent.Visible = false;

Any ideas as to why any postbacks would make the tags appear and in the original page load they do not?

2

There are 2 answers

0
Scott On

The solution offered by Erik won't work for table tags containing property values. For instance: <table align="right"> will not be decoded. I have also found that <img> tags are encoded by the HTMLEditorExtender as well.

The easier solution is to use the Server.HTMLDecode() method.

TextBox_Editor.Text = Server.HtmlDecode(TextBox_Editor.Text) 'fixes encoding bug in ajax:HTMLEditor
0
Erik On

I have the same problem, It seems to have something to do with the default sanitizing that the extension performs on the HTML content. I haven't found a way to switch it off, but the workaround is pretty simple. Write an Anti-Sanitizing function that replaces the cleansed tags with proper tags. Below is mine written in VB.Net. A C# version would look very similar:

 Protected Function FixTableTags(ByVal input As String) As String
    'find all the matching cleansed tags and replace them with correct tags.
    Dim output As String = input

    'replace Cleansed table tags.
    output = output.Replace("&lt;table&gt;", "<table>")
    output = output.Replace("&lt;/table&gt;", "</table>")
    output = output.Replace("&lt;tbody&gt;", "<tbody>")
    output = output.Replace("&lt;/tbody&gt;", "</tbody>")
    output = output.Replace("&lt;tr&gt;", "<tr>")
    output = output.Replace("&lt;td&gt;", "<td>")
    output = output.Replace("&lt;/td&gt;", "</td>")
    output = output.Replace("&lt;/tr&gt;", "</tr>")

    Return output
End Function