Remove rich text format while keeping line breaks in MVC View - c#

I have a field containing rich text formatting where the data is most likely to be a paragraph set out as multiple lines in a list.
I want to show that in a view and remove the formatting but keep the linebreaks so that the list items are still separated.
What I have below (C# within the cshtml) removes the formatting but displays the items on the same line, how can I change this to get what I need, or is there a better way?
MyModel.Synopsis = Regex.Replace(rtfField.ToString(), "<.*?>", String.Empty);

Anyone looking for an answer to this, I just added the following style to the label displaying the data: white-space: pre-wrap;

Related

CKEditor.Text is giving HTMLEncoded Output in ASP.NET

I am trying to use CKEditor into my ASP.NET Application. I got a good resource as follows
http://www.codeproject.com/Tips/532164/How-to-Integrate-CKEditor-with-ASP-NET
http://www.codeproject.com/Tips/455129/Rich-Text-Editor-with-ASP-NET
I want to set the CKEditor Text without losing its formatting (bold, Italics etc..) into a multiline Textbox. So, I am trying the following code.
string str = this.CKEditor1.Text;
TextBox1.Text = str;
Thus its giving me a html encoded Output as follows
<p>dfgdfgfdgdfgdfgdf<strong>gdf</strong></p>
But I don't want to have those tags around but formatting should be preserved. I tried using HTMLEncode and HTMLDecode, Also used this.CKEditor1.HtmlEncodeOutput = false; but of no avail.
Is there any other way I can save the text as it is without losing formatting into my Textbox?
I know textboxes are not meant for storing formatted html outputs but I have to store this(Comments) along with a formatting in a text box(for History) in my Application. Previously they were using Plain text boxes for both comments and History. Now richtext editing is needed and hence we are tying to go this way. Any other good approaches and suggestions are most welcome.
No, a Textbox cant display formatted values. That is why you have the CKEditor.
You can still store the html formated values in the database and display them (formatted) in for example a Label

WPF Textbox - How to save text without the wrapping-paragraph

I load some texts from a database with different lengths into a textbox. (From 1 character to 1000 words...)
I use TextWrapping="Wrap", so if the text is to big for the width of the textbox, it will create a new line....
By saving the text, the added paragraph from TextWrapping="Wrap" will also be saved. But I don't want this. The original text shouldn't be changed in any way...
How can I display the text on a new line in the textbox without adding a "real" paragraph?
Is there a way I can determine if the paragraph was added from TextWrapping="Wrap" or if it belong to the original text?
Thanks
As #StevenRands mentioned, the source text won't be changed in any way by using TextWrapping.
It only affects the display of the text inside the TextBox.
Normally if you used text Wrapping,it wont affect the original source text.But anyways Before saving back to database you can remove the new line constants from your text box text which will give you the original text loaded from db.

Bootstrap Wysiwyg Editor: Saved Data in HTML to Database, trying to send the HTML back to the editor in richtextbox format

So, I have the rich text box editor from Bootstrap-Wysiwyg.
If the textbox contains:
This is a bold statement.
Then my DB will save the text as:
(using a hidden tab where I insert the editor.html())
<b>This is a bold statement.<b>
If I save the file again, the RTF will convert it to:
<b>This is a bold statement.<b>
When I put the this text back into the editor using a Model.Value, it does not convert it to rich text but instead keeps the text as though it were a string and states it exactly as it is saved in the DB.
How can I convert the text in my model to show up in the rich text editors format?
I've been searching the net quite a bit, and most functionality refers to files. This is a new subject to me, so any insight is appreciated on how to fix this. (Editing text without formatting is obnoxious!) Thanks in advance!
Just in case someone else gets stuck, I was able to resolve this issue by doing the following:
I created a hidden div (which I populate first with my Model):
<textarea style="display: none;" id="editorCopy" name="body">#Model.Body</textarea>
and left my editor div empty:
<div id="editor" contenteditable="true">
</div>
and in my JQuery:
I set my editors text to my hidden divs value, and on submit, I replace the hidden div with my editor text:
$('#editor').html($('#editorCopy').val());
$('#postSubmit').click(function () {
$('#editorCopy').val($('#editor').html());
});
This allowed my rich text to show on edit. There might be a better solution, but at least this works! :]

Insert html tag to a Database as a String and retrieve it back in C#

I want to insert html tag to a Database as a String and retrieve it back. after retrieved it ,I want to bind it in Grid.
as a example
String word = "<h1>This is Heading </h1> \n <h2>This is body</h2> \t This is after tab";
after I bind to the grid it should be
This is Heading(in a big font size)
This is body(in small font) - (tab space) This after tab
However this way is not working. It shows
<h1>This is Heading </h1> \n <h2>This is body</h2> \t This is after tab
word instead of applying real HTML behavior. I tryout with '\' special character removal but it remain same result.
Please help me.
Exactly which control are you using? Most of them will escape html like that for you, and to get it to be written 'as-is' you have to set a property.
If you're using GridView, then you can set the HtmlEncode on your BoundColumn to false.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.htmlencode.aspx
If you're using AutoGenerateColumns, see this SO thread: Prevent HTML encoding in auto-generated GridView columns
Saving into DB is not a problem, you can save directly the string into DB,But when displaying on UI you must use HttpUyility.HtmlEncode(), else it will invite Javascript Hacking.
But if you are entering data from WebPage, make sure Input Validations are turned off Else you will get XSS script attack error.
you can bind your text in literal control it will represent the actual HTML formatted text.
like :
ltrlMsg.Text="<div class=\"Message Success\"> Your Message has been sent successfully! </div>"
or you can bind in you grid too.

Response.Write print text from database as HTML

I'm storing text from a textarea to my database. This text can contain new lines, special characters etc. However I want to make sure that when I show it using response.write, it comes out exactly as if it was to show in the textarea control. The reason why I want to do this is because I want to give the user a print view form. The solution below does not work. Using ASP.NET/C#
e.g
foreach (DataRow row in dview.Table.Rows)
{
Response.Write("<tr>");
Response.Write(String.Format("<td>{0}<br/><br/></td>", row[1].ToString().Replace("\n", "<br/>")));
Response.Write("</tr>");
Thanks
Never use "\n" always use Environment.NewLine, instead:
foreach (DataRow row in dview.Table.Rows)
{
Response.Write("<tr>");
Response.Write(String.Format("<td>{0}<br/><br/></td>", row[1].ToString().Replace(Environment.NewLine, "<br/>")));
Response.Write("</tr>");
}
Don't do it that way, you'll run into problems, especially when quotes and symbols are involved.
Store the data like this :
HttpUtility.HtmlEncode("<p>Lorem ipsum ...</p>"); //your HTML to encode goes here
And retrieve it like this:
myPlaceHolder.Text = HttpUtility.HTMLDecode(myData);
Here's a little more information on HTMLEncode/Decode
You could display the output inside a <pre></pre> block which will preserve all whitespace, including multiple spaces, newlines, etc.
HTML encoding is recommended so any angle brackets in the data don't break the output HTML.

Categories

Resources