show line breaks in output - c#

when i enter text into a text area with linebreaks this is not being output later when i display the text. how do i let it show the line spaces?, instead all the text is cramped together at the moment.

How do you display it? HTML ignores whitespace, including line breaks. You can replace the linebreaks with the <br> element to show the enters in your browser too.
Mind that any subsequent spaces are also ignored. You can use the <pre> element too, to display the exact text, but I think that long lines won't be wrapped in that case.

If you are putting your text into HTML try this just before writing it out to the page:
string myText = "" // your text
myText = myText.Replace(Environment.NewLine, "<br/>");

this worked:
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))

Related

How do you include a linebreak taken from a textarea in a paragraph?

I am using a textarea in my view page to store information in a database, then retrieve it to put in a paragraph. Sometimes I have to include a new line in the textarea, but it is always stored in the database as "a b c", when i retrieve the data to put into a paragraph the new line does not appear. It always shows in a horizontal format like "a b c". How can i show the same style in paragraph format?
After you read the text from the database and before you show it in the paragraph, you will need to replace the newlines with HTML line breaks.
string text = textFromDatabase.Replace(Environment.NewLine, "<br />");
Make the textarea wrapped in pre tag
<pre><textarea>CONTENT
CONTENT
</textarea></pre>
Use Pre tag to display the data this will consider the line breaks as entered in the textarea you dont need to put textarea inside pre tag as mentioned in the answer of #user3820407
<pre>
Your Text
</pre>
see http://www.w3schools.com/tags/tag_pre.asp
<p id="paragraph">
</p>
<script>
var ContentFromTextArea = "Your text area content goes here.";
ContentFromTextArea.replace("\r", "<br />");
ContentFromTextArea.replace("\n", "<br />");
$("#paragraph").html(ContentFromTextArea);
</script>

Adding text to email.Body messes with the text formatting

I'm trying to replace some text in the body of the email, but whenever I do it seems to screw up the formatting of the signature text. For example, if I try email.Body.Replace("Info", "Information") then this text:
First Last Title Info
Website: www.stackoverflow.com
turns into:
First Last Title Information Website: HYPERLINK
"http://www.stackoverflow.com/"www.stackoverflow.com
This happens even with just a simple addition to the text like email.Body += "text".
Along with this, it also switches the font of the body from Arial 10 to Calabri 12 (this probably has to do with Calabri 12 being the default email text when Arial 10 is the signiture font).
How would I save the format of the current email body so that when I add text to it, I could set it back or what would be the best way to add text to the body to keep the formatting?
Your help would be welcomed.
MailMessage.Body can be formatted in RTF, which I think it does by default, or in HTML. What the top message you posted actually looks like is something like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\lang9\f0\fs22 First Last \par
Title \par
\par
Info \par
Website: {\field{\*\fldinst{HYPERLINK "www.stackoverflow.com"}}{\fldrslt{\ul\cf1 www.stackoverflow.com}}}\f0\fs22\par
}
I'm assuming that appending "text" to the end of that is causing the RTF message to be parsed improperly, resulting in a loss of formatting.
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.body.aspx
So, what I ended up doing was using the email.HTMLBody. I created a paragraph block similar to what the HTML already had. This is the block: (my HTML isn't very good so... a lot of if is copy/paste with some editing)
<p class=MsoNormal>
<b>
<span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Replace Text
</span>
</b>
</p>
I set this to a string, say htmlPar, then do a replace:
string htmlReplace = htmlPar.Replace("Replace Text", myNewText");
This will create a paragraph block that I can add to the HTML with whatever my text is. To actually add it to the end of the HTMLBody I do a string.Replace like so:
string temp = email.HTMLBody.Replace("</body></html>", htmlReplace + "</body></html>");
email.HTMLBody = temp;
With this I was able to keep the formatting in the rest of the email. Like I said, my HTML isn't great so I'm not sure if this could work to add text in general or how good of a method doing it this way actually is.
Either way, I hope this can help someone else down the road.

How do i remove "System.Windows.Forms.TextBox" from the output text?

text2 = string.Format("[B](No Change)[/B]");
The output comes out as:
System.Windows.Forms.TextBox, Text: (No Change)
How can I remove the System.Windows.Forms.TextBox, Text: from my output?
Well a wild guess, but I think what you are trying to print is not the Text property of the textbox, instead the whole textbox.
Suppose you have:
TextBox1.Text = string.Format("[B](No Change)[/B]");;
Console.Write(TextBox1);
Then you will get the output.
System.Windows.Forms.TextBox, Text: [B](No Change)[/B]
What you probably need to do is to use Text property.
Console.Write(TextBox1.Text);
This will give you the assigned text and will exclude. System.Windows.Forms.TextBox, Text: part
I'm making a guess here but are you trying to make "No change" appear as bold text? In that case, you can modify the textbox font itself rather than try and format your string.
text2.text = "No change";
text2.Font = new Font(text2.Font, FontStyle.Bold);
If that's not what you were trying to do then sorry!
Do not forget to add Text
text2.Text = string.Format("[B](No Change)[/B]");

Replaced InnerHtml Displays as a String

I must be missing the obvious here. I have a drop down menu in a table cell that selects an image to display in that cell after which the drop down menu disappears. I am able to do this with text values in the drop down list by replacing the inner html that holds the drop down list. But when I try to replace the inner html with the location of the image file it just displays as text. Here is the relevant code:
string image = "&ltimg src="" + DropList1.SelectedItem.Value + "" /&gt";
s1.InnerHtml = image;
and the output is:
<img src="D:\Documents and Settings\farmek2\Desktop\Trends\GreenUp.jpeg" />
I need this not to display as text but rather as the image GreenUp.jpg.
Any advice is appreciated.
Regards.
Send the actual markup, not the encoded string.
string image = "<img src=\"" + DropList1.SelectedItem.Value + "\" />";
This is more thank likely because you are injecting HTML special characters into an HTML page. Rather than being rendered as HTML, a browser converts it in the corresponding symbol. Use the actual characters and not their special character equivalents.

How to make string bold in code behind

Is there a way to make a string bolder in code behind using C# .NET?
I tried:
string TypeofDay = "<span style='font-weight: bold'>Type Of Day</span> ";
txtbox.Text = TypeofDay + ": " + "Delivery Day"
I am concatenating TypeofDay(bold) and "Delivery Day" to display in a textbox.
You can't make some bits bold and some bits not bold, in an <asp:TextBox>.
You can't make text bolder by enclosing text value in tags. You must change attribute of a control that displays that text for example by setting its CSS class or changing code-behind property:
txbSendMessageBody.Font.Bold = true;
PS. I am concatenating few bold strings and some other strings to display in a textbox.
A HTML text box does not support this. ASP.NET (usually) generates HTML; if HTML does not support this, you cannot solve it from the server side.
A bit hacky alternative can be to use Unicode bold characters
http://qaz.wtf/u/convert.cgi?text=Type+Of+Day
txtbox.Text = "𝗧𝘆𝗽𝗲 𝗢𝗳 𝗗𝗮𝘆: 𝖣𝖾𝗅𝗂𝗏𝖾𝗋𝗒 𝖣𝖺𝗒"
You could layer a div on top of the textbox. Since the text formatting would change anyway once the user started typing, you can just hide the div once focus is given to the div, thus showing the text box. If nothing is entered and focus is lost, show the div again.
TextBox, no, however you could use a label.
myLabel.text = "<b>bold text</b> normal text <u>underlined text</u> <span style='font-size:Large; color:Red'>Big red text</span>";

Categories

Resources