Rendering HTML in Multiline Textbox (or into any control for that matter)
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.
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>
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.
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
You will need to encode all your tags. For example,
<div> will need to be expressed in your code as: <div>
So, "<" becomes "<" and ">" becomes ">"
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
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.
Here's the answer. Don't use an ASP.NET control such as a textbox. Instead use the suggested div technique (thanks for that).
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
精彩评论