如何在回发中停止 HtmlEditorExtender 编码 html? [英] How do I stop HtmlEditorExtender encoding html in postback?
问题描述
我有一个用户控件,其中包含一个文本框、一个 HtmlEditorExtender
和一个按钮.使用 LoadControl()
将用户控件加载到父页面中.每当我点击按钮发布表单时,文本框中的任何格式化文本都会被编码,这不应该发生.
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.
例如,如果我用
<p>test</p>
我点击按钮发布页面后,.Text
属性返回的文本为
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;
等等.
如果我在设计时将用户控件添加到页面,我确认控件工作正常(不编码 HTML).仅当我使用 LoadControl()
加载它时才会出现此问题.
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.
这是一个简单的例子:
用户控制:
<%@ 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>
用户控制代码(BaseUserControl
扩展了 System.Web.UI.UserControl
并声明了 Initialize()
):
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();
}
}
主页包含这个占位符:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
主页面代码:
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();
}
}
推荐答案
对 this question 似乎是一个不错的解决方法.当您从编辑器中取出文本时,使用 HtmlDecode
对其进行转换:
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);
我也会继续将其发布到您的 codeplex 案例中.(我假设它是你的.)
I'll go ahead and post this on your codeplex case also. (I assume it's yours.)
这篇关于如何在回发中停止 HtmlEditorExtender 编码 html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!