New line character from c# to cshtml - c#

I am making a webpage... the controller is written in C# and the webpage is in cshtml. In the c# file, I am constructing strings to be put into the table on the html page. When I have my strings, I need line breaks within them, so I have things like stringpart1 + "\n" + stringpart2 in hope of the webpage displaying the string as two separate lines as I fill my table cells. However, everything seems to print out on the same line. I have also tried "\r\n" and System.Environment.NewLine instead. Any ideas as to a potential fix?

Here is a fix which worked for me. In your .cshtml, use this:
#Html.Raw(System.Web.HttpUtility.HtmlEncode([your content containing \n]))

Line breaks in HTML are represented by <br> tag. Change \n and/or \r to <br>.
Example:
part1 + "<br>" + part2;

Related

which Newline Character works with Both C# and HTML at a time?

Hello I need Newline Character which works with both i.e C# and HTML
I know That in C# We can use,
System.Environment.NewLine or "\r\n"
and in Html We can use,
<br/>
What is the alternative to this?
That depends on where you intend using that new line. For text files, use NewLine or \r\n. For HTML documents, use <br/>.
As you wrote in the comments:
I want to write log in log file and also send that log via mail as html body.
So you would need to seperate the two cases.
For breaking a line in your log file, use \r\n or System.Environment.NewLine.
For breaking a line in your HTML body, use <br/>.
For using the log as an HTML document body, you can simply use:
string body = logFileConent.Replace(#"\r\n", "<br/>");
Where logFileConent is a string variable holding your text file lines.
Allright, use this type of strings:
"Debug: This is Debug 1 \r\n <br> Debug: This is Debug 2"
It will always display the <br> in the log file:
But the e-mail will only display a line break, if it displays HTML normally:

Display Code From A Database with C#

I have a database with almost 2000 code examples that I've collected over the last 10 years that I want to be able to access with a textarea field so I can copy and/or update easily. I'm trying to migrate from PHP to C# on a new domain, but I'm having problems getting it to display correctly.
This is how it looks currently: http://nunyabiz.freeiz.com/csharp/index.html
This is how it's supposed to look: http://nunyabiz.freeiz.com/csharp/index2.html
This is the code that I'm using to display it:
Code.Text = rs["Code"].ToString().Replace("\r\n", "\n");
The "\r\n" characters aren't getting recognized, so I'm guessing that I have to convert, encode or decode it, but I haven't had any luck trying to find something that will work. online.
you can try Environment.NewLine for Linebreaks Environment.Newline
strYourCode = string.Format("Name: {0} LineBreak {1}", yournameVariable, Environment.NewLine);
Try to figure out WHAT line breaks you have in your code samples - there are many variants including html tag br, and then change it to what you need (i think it is Environment.Newline)
And check that your Code control is Multiline
Give this a try .Replace("\r", "").Replace("\n", "<br/>");

Getting NewLine character in FreeTextBox control in Asp.net

I am using FreeTextBox control
in asp.net.When I am getting its HtmlStrippedText in my code I am getting the String without HTML tags.Now how can I get the new line character from this String i.e. I want to Replace All the NewLine characters with Special Symbol "#".
Got the Solution:
Got the HtmlStrippedText in String str and then got replace it like this:
char enter=(char)111;
temp= str.Replace(enter+"", "\n");
If it is anything like the base ASP:TextBox, you can just grab the string from the text property and do something like
var test = txtYourControl.Text.Replace(Environment.NewLine, "#");
This will vary between browsers (perhaps even between the operating systems on which the browser is running). More on newline definitions here.
I have found the most reliable option to be to search and replace "\n" rather than Environment.NewLine which is "\r\n" in a Windows environment.
string text = this.txtField.Text.Replace("\n", "#");
HtmlStrippedText is used to get the plain text
You should use the text property of freetextbox control
u can remove new line using this code.
lblTitle.Text = txtFreetextbox.Text.Replace("<br>","#");
Click this link
http://freetextbox.com/docs/ftb3/html/P_FreeTextBoxControls_FreeTextBox_Text.htm

Write a new line to a web site using System.Web.HttpUtility.HtmlEncode in C#

I'm trying to write some text to a website using the System.Web.HttpUtility.HtmlEncode function in C#. The string parameter that I give to the function has some Environment.Newline's in it, but they are not written out on the website. Does anyone know why this is and how I can fix it. Thanks in advance.
Newline is written out as a physical line break so you will have to either wrap in a pre:
response.Write("<pre>" + HttpUtility.HtmlEncode(str) + "</pre>");
Or replace the new line with a BR AFTER you have HtmlEncoded (or it'll encode the BR as well):
response.Write(HttpUtility.HtmlEncode(str).Replace("\n", "<br />"));
Given its HTML NewLine characters do not display as whitespace. Try replacing your new line characters with <br/> elements.

What's with the line break variations in C# and ASP.NET? (\r\n vs.\n)

I've been writing code for ASP.NET since the start, and today I encountered something I've never seen before. Typically I've looked for line breaks in C# (when posted in ASP.NET from a textarea, for example) by expecting "\r\n". Now I'm using the MVC framework, and the text coming over the wire from a textarea simply has "\n" for line breaks.
Is there something going on in old school TextBox controls that normalizes the line breaks? Since MVC just uses the form data as-is, is this normal? Would love to get some insight.
I have made observation that the actual line break sequence differs from browser to browser.
If you have a multiline textarea on a page and the page is submitted then:
IE returns "\r\n" to indicate newlines. FF returns "\n" in this case.
I tested it somewhere along the end of 2006, so it may be different now.
I do not believe it could have anything to do with WebForms vs. MVC. Both just process submitted input and return it to you as it is.
If you wish to somehow process and replace these characters it would make sense doing it in the long-to-short order:
string userText = form["Text"];
userText = userText.Replace ("\r\n", "<br/>").Replace ("\r", "<br/>");
\n is the line ending variant on *nix-style systems. \r\n is Windows-specific line-ending behaviour.
If you're checking for line-endings and expose your interface to non-Windows environments, be sure to check not only for \r\n but also for \n alone.
Fore more background, check out the Newline article at Wikipedia.
I am using Environment.NewLine :
string userText = form["Text"];
userText = userText.Replace (Environment.NewLine, "<br />")
Also take a look at #Haacked's post about some newline textarea quirks.

Categories