Response.Write print text from database as HTML - c#

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.

Related

Remove rich text format while keeping line breaks in MVC View

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;

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

Fill word Content Control using C# issue

I have word contains content control like
I want to write many lines on it so in C# I tried many ways :
using EnviromentNewLine .
\n
Example:
for (int i = 0; i < items.Count; i++)
{
items[i] = string.Format("{0}{1}{2}{3}", (i + 1).ToString(CultureInfo.InvariantCulture), ". ", items[i], "<br />");
}
but I still get the word content control on one line , any idea how to fix it
The actual problem is, that the plain text content control does not support multiple paragraphs, it is only possibly to insert manual line breaks (which you normally do by pressing Shift+Return). Within the plain text content control, Word automatically replaces Return by Shift+Return as you can see when switching on the formatting symbols.
You basically have two options to solve your issue:
Change the content control to a rich text content control. That way you will be able to insert arbitrary content, also new line characters (i.e. multiple actual Word paragraphs)
Insert a vertical tab, i.e. ASCII character 11 or \B instead of a new line. This will insert the manual line break required by the plain text content control.
Try
\r
MSDN 2.4.4.4 Character literals

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.

How to display data inside a textbox?

My question is that I have extracted data from a file and pushed into a string variable.
I have a FORM created in visual studio having a text box, so I want to display that extracted data in textbox created.
How can achieve this?
You can do it like this:
yourTextBox.Text = yourString;
For a TextBox, it is just
myTextBox.Text = myStringVariable;
But if the data is large, you may want to consider the RichTextBox control instead.
This is all assuming you're using Windows Forms...
string strName="someone";
TextBox1.Text=strName;
If you trim the data to remove the unwanted trailing spaces if there is any.
TextBox1.Text=strName.Trim();

Categories

Resources