3

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

<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. 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(); 
    } 
} 
sashkello
  • 17,306
  • 24
  • 81
  • 109
Dan C
  • 2,236
  • 2
  • 19
  • 30
  • I could never get this to work reliably for dynamically loaded scenarios so I ended up moving the control to a simple page that was loaded into an iframe. This way the control was static on a page and so behaved properly. I just had to deal with transferring the data back and forth between the parent page and control page. – Dan C Nov 07 '12 at 18:40

4 Answers4

1

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.)

Community
  • 1
  • 1
Matthew Lowe
  • 1,350
  • 1
  • 17
  • 29
  • I tried this, but unfortunately it is not reliable. There were cases during postback when this caused decoding when none should happen. This would incorrectly alter the user's text and destroy the formatting. – Dan C Nov 07 '12 at 18:37
  • Bummer. For my benefit, could you describe one of the cases? – Matthew Lowe Nov 08 '12 at 19:33
0

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

JohnM
  • 1
  • 1
  • The control is designed to take in HTML and be able to edit it. The issue is that this encoding problem only happens when I use LoadControl() to load this control. If I drag and drop it onto a form directly everything works fine and on postbacks I get the HTML back un-encoded. – Dan C Jun 08 '12 at 06:03
0

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.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
JohnM
  • 1
  • Thank you for the suggestion. However, the issue with this solution is that ViewState is not available and in the actual web site I use ViewState to hold information to determine how many of those user controls to load into the page. I had to move the LoadControl to the Page_PreLoad but I still had the same issue where it encoded the text on postback. – Dan C Jun 13 '12 at 06:09
-1

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
xsari3x
  • 442
  • 2
  • 12
  • 36