Friday, March 9, 2012

Keeping text format after stored in the data base.

I want the users of my site to be able to write a couple of paragraphs, save it to SQL Server and then have it read back and look the same. I don't want the users to be able to add any html to the text they submit. I just want them to be able to seperate or indent their paragraphs.

At one point, I had a textbox that saved text to the server and read it back the same way it was originally inputed. However, I can't figure our what I had done to make it work.

Any ideas?
ThanksIt's usually an issue between cariage return and line feed characters and <br> tags. Depending on where/how you're displaying the text, you probably have to replace one with the other.|||For those that are interested.
I figured out that I can look at the ASCII value of each Char in the string then use a switch statement
public string FormatText(string textBlock)
{
//return textBlock;4444
string formattedText = "";
int charCount = 0;
foreach(char x in textBlock)
{
charCount++;
switch((int)x)
{
case 10:
charCount = 0;
formattedText += "<br>";
break;
case 32:
if(charCount > 60)
{
formattedText += "<br>";
charCount = 0;
}
else
formattedText += " ";
break;
default:
formattedText += x;
break;
}
}
return formattedText;
}

No comments:

Post a Comment