Answered by:
Formatted text in <P> Tag

Question
-
User158811806 posted
The following formatted text is saved in a database.
How do I display it in an aps.net's <P> tag exactly the way it shows below?
Welcome to American Saint Miguel, you may now request to online if your lease does not expire within 10 -15 days of your request. To be considered eligible to lease, you must: (1) You have not rented before, and (2) You must be in “good standing”. Note: If your account is delinquent, you will not be able to lease and your application fee will not be refunded. Good standing means that you do not owe money, you do not have any past due amount to creditors, and your lease expires within 10 - 15 days. If you do not meet the good standing criteria, your application will not be approved. If you meet the above mentioned criteria, please click here for details on required documents that you will need to submit.
Friday, July 24, 2020 2:44 AM
Answers
-
User475983607 posted
njehan01
still not working, displays <br /> in the paragraph.
This is the entire code for the paragraph, what's not here is the sql connection code whihc should not matter in this case.
<div id="nd-boxA" class="nd-box"> <p id="paragraph3" runat="server"></p> </div>
reader.Read(); string sLabel3 = reader["FieldValue"].ToString(); sLabel3 = sLabel3.ToString().Replace("\r\n", "<br />").Replace("\n", "<br />"); if (sLabel3 != null) { this.paragraph3.InnerText = Server.HtmlDecode(sLabel3); }
IMHO, the design should not allow white space characters (\r\n) to make their way to the database in the first place.
<div id="nd-boxA" class="nd-box"> <p> <asp:Literal ID="paragraph3" Mode="PassThrough" runat="server"></asp:Literal> </p> </div>
protected void Page_Load(object sender, EventArgs e)
{
string text = "Hello\r\nWorld";
paragraph3.Text = text.Replace("\r\n", "<br />").Replace("\n", "<br />").Replace("\r", "<br />");
}If this still does not work then you have issues elsewhere in the code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, July 25, 2020 3:42 PM
All replies
-
User158811806 posted
I have tried the following code but it makes <P> tag to display literal "<\br>"
string sLabel = reader["FieldValue"].ToString().Replace("\n", "<br/>"); //string sLabel = reader["FieldValue"].ToString(); if (sLabel != null) { this.paragraph.InnerText = sLabel; // I also tried all the following with no luck //sLabel = sLabel.Replace(Environment.NewLine, "<br />"); //sLabel = sLabel.Replace(" ", " "); //this.paragraph.InnerText = //System.Text.RegularExpressions.Regex.Replace(sLabel, @"\r\n?|\n", "<br />"); }
Friday, July 24, 2020 5:22 AM -
User288213138 posted
Hi njehan01,
string sLabel = reader["FieldValue"].ToString().Replace("\n", "<br/>");
You can refer to this demo:
<p id="pid" runat="server"></p> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> protected void Button1_Click(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { conn.Open(); string query = "SELECT * FROM Test8"; SqlCommand cmd = new SqlCommand(query, conn); Object result = cmd.ExecuteScalar(); pid.InnerHtml = result.ToString().Replace("\r\n", "<br />").Replace("\n", "<br />"); } }
The result:
Best regards,
Sam
Friday, July 24, 2020 8:13 AM -
User158811806 posted
I have tried both in Chrome and IE, it displays literal <br/>
<form class="formClass" id="form1" action="./MainPage.aspx" method="post">expire within 90 -120 days of your request, your request to will be denied. < br/>To be officially considered eligible to lease, you must:< br/>(1) have not ........
Friday, July 24, 2020 12:54 PM -
User475983607 posted
The framework encodes HTML characters to protect you from malicious script. Either decode the HTML or use the Literal server control to render HTML content.
<asp:Literal ID="Literal1" Mode="PassThrough" runat="server"></asp:Literal>
Literal1.Text = "<p>Hello<br />World</p>";
Or
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Label1.Text = Server.HtmlDecode("<p>Foo<br />Bar</p>");
Friday, July 24, 2020 1:17 PM -
User158811806 posted
How do I do it in my code below?
string sLabel3 = reader["FieldValue"].ToString();
sLabel3 = sLabel3.ToString().Replace("\r\n", "<br />").Replace("\n", "<br />");
this.paragraph3.InnerText = sLabel3;Saturday, July 25, 2020 5:53 AM -
User475983607 posted
How do I do it in my code below?
string sLabel3 = reader["FieldValue"].ToString();
sLabel3 = sLabel3.ToString().Replace("\r\n", "<br />").Replace("\n", "<br />");
this.paragraph3.InnerText = sLabel3;I assume here....
this.paragraph3.InnerText = Server.HtmlDecode(sLabel3);
Unfortunately, we cannot see all your code and have no idea how your code works.
Saturday, July 25, 2020 11:21 AM -
User158811806 posted
still not working, displays <br /> in the paragraph.
This is the entire code for the paragraph, what's not here is the sql connection code whihc should not matter in this case.
<div id="nd-boxA" class="nd-box"> <p id="paragraph3" runat="server"></p> </div>
reader.Read(); string sLabel3 = reader["FieldValue"].ToString(); sLabel3 = sLabel3.ToString().Replace("\r\n", "<br />").Replace("\n", "<br />"); if (sLabel3 != null) { this.paragraph3.InnerText = Server.HtmlDecode(sLabel3); }
Saturday, July 25, 2020 3:17 PM -
User475983607 posted
njehan01
still not working, displays <br /> in the paragraph.
This is the entire code for the paragraph, what's not here is the sql connection code whihc should not matter in this case.
<div id="nd-boxA" class="nd-box"> <p id="paragraph3" runat="server"></p> </div>
reader.Read(); string sLabel3 = reader["FieldValue"].ToString(); sLabel3 = sLabel3.ToString().Replace("\r\n", "<br />").Replace("\n", "<br />"); if (sLabel3 != null) { this.paragraph3.InnerText = Server.HtmlDecode(sLabel3); }
IMHO, the design should not allow white space characters (\r\n) to make their way to the database in the first place.
<div id="nd-boxA" class="nd-box"> <p> <asp:Literal ID="paragraph3" Mode="PassThrough" runat="server"></asp:Literal> </p> </div>
protected void Page_Load(object sender, EventArgs e)
{
string text = "Hello\r\nWorld";
paragraph3.Text = text.Replace("\r\n", "<br />").Replace("\n", "<br />").Replace("\r", "<br />");
}If this still does not work then you have issues elsewhere in the code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, July 25, 2020 3:42 PM -
User158811806 posted
It seems that the <P> tag needs a <Literal> tag for it to format.
I put a Literal inside the Paragraph tag which formatted the text properly, this was a news to me as it was not referenced anywhere when I Googled.
Thanks for your help.
Sunday, July 26, 2020 4:40 AM -
User475983607 posted
It seems that the <P> tag needs a <Literal> tag for it to format.
I put a Literal inside the Paragraph tag which formatted the text properly, this was a news to me as it was not referenced anywhere when I Googled.
Thanks for your help.
You are mistaken. You used innerText not innerHTML. I used a literal because ASP.NET Web Forms is designed to use server controls.
protected void Page_Load(object sender, EventArgs e) { string text = "Hello\nWorld".Replace("\r\n", "<br />").Replace("\n", "<br />").Replace("\r", "<br />"); paragraph3.Text = text; Label1.Text = Server.HtmlDecode(text); p1.InnerHtml = Server.HtmlDecode(text); p2.InnerText = Server.HtmlDecode(text); //encodes the HTML as expected }
<div> <asp:Literal ID="paragraph3" Mode="PassThrough" runat="server"></asp:Literal> </div> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> <div> <p id="p1" runat="server"></p> </div> <div> <p id="p2" runat="server"></p> </div>
Sunday, July 26, 2020 10:57 AM