I found a case where the HtmlEditorExtender is encoding the HTML during a postback when it's in a user control that is loaded onto a page using LoadControl().
To reproduce, create a user control with a text box, an HtmlEditorExtender, and a button. Create a web page and in the Page_Load event, use LoadControl() to load the user control.
If you load or enter formatted text into the text box, for example "<p>test</p>" and click the button to post the page, the text is replaced with "<p>test</p>". Clicking the button again, further encodes that and replaces it with "&lt;p&gt;test&lt;/p&gt;". This gets further encoded each postback.
I confirmed that this does not happen if I add the user control at design time to the page, only if I use LoadControl() to load it.
I have spent days trying to resolve this issue, but I just can't tell if I am doing something wrong, if the control is simply incompatible with this scenario, or if there is a reliable workaround.
Here is some code to reproduce the issue:
User control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestDynamicRichTextControl.ascx.cs" Inherits="Sample.forms.TestDynamicRichTextControl" %>
<asp:TextBox ID="txtBody" runat="server" Columns="80" Rows="15" TextMode="MultiLine"></asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="heeBody" runat="server"
TargetControlID="txtBody">
<Toolbar>
<ajaxToolkit:Bold />
<ajaxToolkit:Italic />
<ajaxToolkit:Underline />
</Toolbar>
</ajaxToolkit:HtmlEditorExtender>
<br />
<asp:Button ID="btnTestPartialPostback" runat="server" Text="Test Partial Postback" onclick="btnTestPartialPostback_Click" />
<asp:Label ID="lblResult" runat="server"></asp:Label>
User control code (BaseUserControl extends System.Web.UI.UserControl and declares Initialize() ) :
public partial class TestDynamicRichTextControl : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void Initialize()
{
txtBody.Text = "<p>This is a test</p>";
}
protected void btnTestPartialPostback_Click(object sender, EventArgs e)
{
lblResult.Text = DateTime.Now.ToString();
}
}
The main page contains this placeholder:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
The code of the main page:
public partial class TestDynamicControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Clear();
BaseUserControl formUc = (BaseUserControl)this.LoadControl("forms/TestDynamicRichTextControl.ascx");
PlaceHolder1.Controls.Add(formUc);
if (!IsPostBack)
formUc.Initialize();
}
}
Comments: Workaround: When you get the text out of the editor, use HtmlDecode to convert it: String fixedText = HttpUtility.HtmlDecode(txtBody.Text);
To reproduce, create a user control with a text box, an HtmlEditorExtender, and a button. Create a web page and in the Page_Load event, use LoadControl() to load the user control.
If you load or enter formatted text into the text box, for example "<p>test</p>" and click the button to post the page, the text is replaced with "<p>test</p>". Clicking the button again, further encodes that and replaces it with "&lt;p&gt;test&lt;/p&gt;". This gets further encoded each postback.
I confirmed that this does not happen if I add the user control at design time to the page, only if I use LoadControl() to load it.
I have spent days trying to resolve this issue, but I just can't tell if I am doing something wrong, if the control is simply incompatible with this scenario, or if there is a reliable workaround.
Here is some code to reproduce the issue:
User control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestDynamicRichTextControl.ascx.cs" Inherits="Sample.forms.TestDynamicRichTextControl" %>
<asp:TextBox ID="txtBody" runat="server" Columns="80" Rows="15" TextMode="MultiLine"></asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="heeBody" runat="server"
TargetControlID="txtBody">
<Toolbar>
<ajaxToolkit:Bold />
<ajaxToolkit:Italic />
<ajaxToolkit:Underline />
</Toolbar>
</ajaxToolkit:HtmlEditorExtender>
<br />
<asp:Button ID="btnTestPartialPostback" runat="server" Text="Test Partial Postback" onclick="btnTestPartialPostback_Click" />
<asp:Label ID="lblResult" runat="server"></asp:Label>
User control code (BaseUserControl extends System.Web.UI.UserControl and declares Initialize() ) :
public partial class TestDynamicRichTextControl : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void Initialize()
{
txtBody.Text = "<p>This is a test</p>";
}
protected void btnTestPartialPostback_Click(object sender, EventArgs e)
{
lblResult.Text = DateTime.Now.ToString();
}
}
The main page contains this placeholder:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
The code of the main page:
public partial class TestDynamicControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Clear();
BaseUserControl formUc = (BaseUserControl)this.LoadControl("forms/TestDynamicRichTextControl.ascx");
PlaceHolder1.Controls.Add(formUc);
if (!IsPostBack)
formUc.Initialize();
}
}
Comments: Workaround: When you get the text out of the editor, use HtmlDecode to convert it: String fixedText = HttpUtility.HtmlDecode(txtBody.Text);