I have a user control that contains a text box, an HtmlEditorExtender
, and a button. The user control is loaded into a parent page using LoadControl()
. Whenever I click on the button to post the form, any formatted text in the text box gets encoded, which is not what should happen.
For example, if I load the text control with
<p>test</p>
after I click on the button to post the page, the text returned by the .Text
property is
&lt;p&gt;test&lt;/p&gt;
If I post a second time, it is further encoded as:
&amp;lt;p&amp;gt;test&amp;lt;/p&amp;gt;
and so on.
I confirmed that the control works fine (does not encode the HTML) if I add the user control at design time to the page. This issue only happens 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 a simple example:
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>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();
}
}
My understanding of the HTML extender is that the HTML tags are added based on the tools used on the toolbar. Have you tried loading the tb.text with:
as opposed to
As to why it is doubling up on you like that, if the extender is adding the tags with every update then it seems as though they would(should) keep being added with every iteration.
...put the "p" tags around the textbox in markup