I have a generic html control - a 'P' tag, which i've added a runat="server" to.
I construct a string using Stringbuilder methods and apply this to the InnerHtml attribute of the tag.
The text,which contains a number of sb.Appendline()'s still displays as a paragraph would, wrapping but without creating any new lines. However, When i look at the markup created, the new lines are there, shown as spacing.
This is also the case with a DIV
Anyone got any ideas as to how i fix this?
Code below:
var sb = new StringBuilder();
sb.Append("Congratulations!");
sb.AppendLine();
sb.AppendLine();
sb.AppendFormat("Your application was accepted by <b>{0}</b>.", response.AcceptedLender);
sb.AppendLine();
sb.AppendFormat("Your reference number is <b>{0}</b>.", response.PPDReference);
sb.AppendLine();
sb.AppendLine();
sb.Append("Click the button below to sign your loan agreement electronically and collect your cash.");
AcceptedMessage.InnerHtml = sb.ToString();
Instead of using AppendLine should use AppendFormat:
myStringBuilder.AppendFormat("{0}<br />", strToAppend);
If you need these line breaks to remain "as is" because you're displaying it in a text file or something of that nature, you'll need to replace the new line text with html breaks when you're calling the .ToString() method.
myStringBuilder.Replace(Environment.NewLine, "<br />").ToString();
Using Environment.NewLine here may not work, you may actually need to replace \r\n.
You have to convert the stringlbuilder newlines (\n) into BR tags
so that they can be rendered in HTML newlines.
Even better IMO is to enclose the whole lines in P tags
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With