2

I added some text to my multiline textbox. It's got some Html tags such as ,

, etc. How do I format this so that the text inside the multi-line textbox renders to the browser with the formatted HTML? Right now it's just rendering the plain text.

some example text that was appended:

"<p class=""myclass"">blah blah blah some text</p>"

I tried this but it just renders the encoded values, doesn't render them as HTML:

txtSomeMultilineTextbox.Text = HttpUtility.HtmlEncode(someText.ToString());

The purpose of this multiline textbox will be for a read-only Terms & Conditions box. I want formatted text in there such as bold, etc.

I'm not interested in using a 3rd party control. Just want to figure out how to get this working.

0

8 Answers 8

5

I assume you want to use a multiline textbox because of the scroll bar. You can achive this with a div.

<div style="width:300px;height:250px;overflow:auto;"></div>
2
  • Yes, because of the scroll bar. Most terms & conditions are long, no way I'm showing the entire thing down the page on our small web form. Oct 19, 2009 at 21:38
  • I cannot imagine now using any type of control in ASP.NET when you can use simple HTML & CSS. Good god. Thanks, did not know this was possible! Oct 20, 2009 at 13:31
1

Formatted HTML doesn't work in the standard HTML textarea control. Getting this to work is really tricky and most implementations rely on iFrames and heavy javascripting. Trying to implement this on your own is a really bad idea.

Fortunately there is TinyMCE which is open source and pretty awesome.

1

If this is for read only terms and conditions, I'm assuming you want it in a textbox so it scrolls? You can achieve this with a normal div, fix the height and use the overflow:auto css property

7
  • So I tried to set the div's InnerText from code-behind, that did not produce a scrollbar when rendered: <div id="scrollableDiv_300x100" runat="server"></div>. It rendered the entire thing instead, no scroll bar Oct 19, 2009 at 21:46
  • I'm assuming you've put the a CSS class on it? If you're going by the id of the item to style it remember that ASP.NET will mangle the name of the div to $ctl_100xxblahblah. Oct 19, 2009 at 22:07
  • <div id="scrollableDiv_300x100" runat="server"></div>. The ID has that style in it with the overflow:auto, shouldn't matter if it's an ID or class in this case. Oct 20, 2009 at 1:15
  • here's the id in CSS: (borrowed from previous poster): #scrollableDiv_300x100 { width:300px; height:100px; overflow:auto; } Oct 20, 2009 at 1:15
  • that does not give me any scroll bar when I set its text from code-behind. Oct 20, 2009 at 1:16
0

You will need to encode all your tags. For example, <div> will need to be expressed in your code as: &lt;div&gt;

So, "<" becomes "&lt;" and ">" becomes "&gt;"

3
  • so basically I need to UrlEncode the text before binding to my ASP.NET control and then do a find/replace? I don't get it. Oct 19, 2009 at 19:14
  • I encoded that text before binding to my textbox. It rendered as the encoded tags, not as html. So the output in my textbox was actually a bunch of &gt; etc. rendered, not converted to HTML in that multiline box after wrapping the text I was binding to the control with HttpUtility.HtmlEncode Oct 19, 2009 at 19:29
  • so for example this rendered the encoded values, not HTML when I viewed the rendered textbox in my browser: txtSomeMultilineTextbox.Text = HttpUtility.HtmlEncode(theText.ToString()); Oct 19, 2009 at 19:30
0

In order to view HTML as HTML and have the textbox that is aware of HTML tags you are going to have to use a HTMLTextbox. FCKEditor is a popular and easy to use editor.

Also, Don't forget set validaterequest="false" on your page directive to disable the automatic HTML injection testing

0

I'd escape the reserved HTML characters (e.g. <, >, &, and ') and see if that allows the data to display. You can find a complete reference here.

Personally, though, I rarely do this through a textarea. Instead, I use ckEditor (formerly fckEditor) to display and allow editing of richly formatted text.

1
  • thanks, we do not want to use a 3rd party control for a simple multiline textbox. Oct 19, 2009 at 19:21
0

Here's the answer. Don't use an ASP.NET control such as a textbox. Instead use the suggested div technique (thanks for that).

0

Since what I see are work-arounds. The text attribute on the textbox control is html-encoded so no matter what html you set the textbox.text to, it will display literally what you set it to.

The "answers" suggested here are more or less work arounds as you don't maintain the control and instead you're using another method all together(HTML). If you had code behind tied to this you would have to rewrite it to use ajax or w/e to facilitate the functionality from the code behind.

Instead of to \r\n etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.