Retrieve database content without linebreak - c#

(asp.net C# project)
I am trying to show contact info in my footer, I receive the info from the database, but in my administrator panel I have CKEditor which adds linebreaks. So, what my problem is that I'd like my text in my footer to have no linebreak, is there any way of doing this?
How it looks in my contact page:
Adresse
Zip, City
Phone
Fax
How I would like it to be in my footer:
Adresse, Zip city, phone, fax
I receive the texts using:
<p><asp:Literal ID="LitContent" runat="server" /></p>
LitContent.Text = textService.GetText("ContactInfo");

LitContent.Text = textService.GetText("ContactInfo").Replace("<br />", " ");
but you should check how the BR is formatted from the editor. It may be capitalized or without /
It is also possible that the HTML generated by the editor uses paragraphs instead of br. In this case it becomes more complex to remove them. Basically to answer your question correctly we need more information. What is the actual string that GetText returns.

This looks like a formatting Problem.
There's a string and you can replace the Linebreaks out of a string. The code shown below do this by replacing the Linebreak with a blank. Should be the easiest way of doing this
infoText = textServices.GetText("ContactInfo");
infoText = infoText.Replace(Environment.NewLine, " ");
LitContent.Text = infoText;

Related

How can i get and insert formatted text from mysql database and use it

I'm trying to create a news feed for my website. Right now the news column is of type longtext. I've varchar but it didn't work as well. There are a few rows with formatted text. I mean they have "carriage return". I get data from the database in code behind and set them to sessions. In design page I've tried this:
<div>sessioncode.toString()</div>
<div>sessioncode</div>
<div class="panel-body">
<%:Session["HaberIcerik"]%>
</div>
Both ignored \r (carriage return). The <a> tag looks like a normal <a> string. In code behind I did this:
sessionvalue = sqldatas.rows[0]["newscontent"];
sessionvalue = sqldatas.rows[0]["newscontent"].toString();
Session["HaberIcerik"] = haber.Rows[0]["haberIcerik"];
What are your suggestions?
A web browser will convert and condense new-lines to a whitespace as mandated in the HTML specification which requires <br /> be used to indicate a line break.
Either pre-process the string before rendering:
sessionvalue = Regex.Replace(input_string, #"\r\n?|\n", "<br />");
Or render as-is but within an element that has the white-space: pre CSS style.

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.

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.

show line breaks in output

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 />"))

Categories

Resources