简体   繁体   English

如何在回发中停止HtmlEditorExtender编码html?

[英]How do I stop HtmlEditorExtender encoding html in postback?

I have a user control that contains a text box, an HtmlEditorExtender , and a button. 我有一个用户控件,其中包含一个文本框,一个HtmlEditorExtender和一个按钮。 The user control is loaded into a parent page using LoadControl() . 使用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 单击按钮发布页面后, .Text属性返回的文本为

<p>test</p> 

If I post a second time, it is further encoded as: 如果我第二次发布,则进一步编码为:

<p>test</p> 

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. 如果我在设计时将用户控件添加到页面,我确认控件工作正常(不编码HTML)。 This issue only happens if I use LoadControl() to load it. 只有在我使用LoadControl()加载它时才会出现此问题。

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() ): 用户控制代码( BaseUserControl扩展System.Web.UI.UserControl并声明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: 当您从编辑器中获取文本时,使用HtmlDecode进行转换:

    String fixedText = HttpUtility.HtmlDecode(txtBody.Text);

I'll go ahead and post this on your codeplex case also. 我会继续发布你的codeplex案例。 (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. 我对HTML扩展器的理解是,HTML标签是根据工具栏上使用的工具添加的。 Have you tried loading the tb.text with: 您是否尝试使用以下命令加载tb.text:

"This is a test" “这是一个测验”

as opposed to 而不是

"< p >This is a test< /p >" “<p>这是一个测试</ 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 ...在标记中的文本框周围放置“p”标签

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. 看起来好像将“加载”事件中的主页代码放入“Init”事件中就可以了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM