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();
}
}
The response to this question seems to be a decent workaround. When you get the text out of the editor, use HtmlDecode
to convert it:
String fixedText = HttpUtility.HtmlDecode(txtBody.Text);
I'll go ahead and post this on your codeplex case also. (I assume it's yours.)
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:
"This is a test"
as opposed to
"< p >This is a test< /p >"
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
Anyway I actually had a chance to test this and your problem was recreated. It looks as though putting your main page code that is in the "Load" event into the "Init" event it works just fine.
I was able to solve this problem like this :
Literal lit = new Literal();
lit.Mode = LiteralMode.PassThrough;
lit.Text = HttpUtility.HtmlDecode(HTMLTExt);
TextBox1.Text = lit.Text; // The text box which HTMLEditorExtender is attached to
来源:https://stackoverflow.com/questions/10825791/how-do-i-stop-htmleditorextender-encoding-html-in-postback