I want to format the content before showing to web page. I am storing '\r\n\' as enter in Database and trying to replace it before show content on the web page. server side my code is:
lblComments.Text = Server.HtmlEncode(((DataRowView)e.Item.DataItem).Row["Comment"].ToString().Replace("\r\n", "<br>"));
I have use Server.HtmlEncode.
My Out put should be:
Comment Type: Public
Comment: Commented on the Data by user A
But, its shows me everything in single line.
You need to use
.Replace("\r\n", "<br/>")
And only AFTER Server.HtmlEncode, because you don't want the <br/> itself to get encoded to <br/> (raw), displaying as <br/> literally.
You could wrap the output in a <pre>...</pre> element instead of replacing the line breaks with <br>. That way, the line breaks should be conserved.
E.g. put a <pre> element around your Label:
<pre><asp:Label id="lblComments" runat="server" /></pre>
Try
Replace("\n", "<br>")
This test worked without encode
string comments = #"Comment Type: Public
Comment: Commented on the Data by user A";
lblComments.Text = comments.Replace("\n","<br>");
Related
I have a script I am building that creates ASP Classic pages from form input.
I am using the file object to create the asp file. It is working as intended, however I am slightly perplexed how to embed ASP CLASSIC code into the string. The open tag works like this:
fname.WriteLine"<% DIM GENERIC_VAR"
This displays in the file properly, however the use of the close tag doesn't seem to want to work. Even my IDE is indicating a formatting issue. Usually in the instances I need to replace a " (double quote) a ' (single quote) will work, but in this case I get compile errors (Unterminated string constant) or the file doesn't create the line as expected. I know about doubling up "" but so far haven't had any luck. Thanks.
Side note as an example, I just need to be able to print this into the line of the file:
fname.WriteLine"%>"
openasp = "<"
openasp = openasp & "% "
closeasp = " %"
closeasp = closeasp & ">"
Set up the ASP tags like above.
Then Wrap them in line as needed for example:
fname.WriteLine openasp & "DIM VAR_NAME" & closeasp
I need to extract to clipboard activation link, link every registration changed.
HTML Code:
Try something like this code:
SEARCH SOURCE=REGEXP:"(http://mctop.me/approve/\w+)" EXTRACT=$1
SET !CLIPBOARD {{!EXTRACT}}
Error -1200: parses "(http://mctop.me/approve/\w+)" - Unrecognized esc-sequence \w.
I have a multi line text box like :
<asp:TextBox CssClass="txtform" ID="txtWhom" runat="server" Height="78px"
Rows="10" TextMode="MultiLine"></asp:TextBox>
say I write the following text in that text box :
Dear Sir,
General Manager,
HSBC.
when I take that text in a variable in the vb.net code behind... and send it to a crystal report to show ... only ... the first line is shown... in that case only "Dear Sir," is shown ... but I want all the text ....
What Can I do ?
Use "Can Grow" option for that text object in Crystal report.
=>Right Click -> Text Object
=>Select Format Object
=>Use Can grow option in the dialog box.
It enables for variable length fields to grow vertically in the report and word wrap automatically. A maximum number of lines can be set with this option to control rogue or large data elements.
I wonder if your entered string includes carriage return & line feed chars that Crystal is not dealing with properly. You may need to strip those out or replace them with correct characters that Crystal expects.
When I try and display text from an the InnerText from an XML element. I get something like this:
I need this spacing \r\n\r\n\r\second lot of spacing\r\n\r\nMore spacing\r\n\r\n
I know you can replace \r\n with <br> but is there no function that automatically takes the html for you and why does it use \r and \n? Many thanks.
You can use <pre> tag - it will show the text as-is like you see it in text editor:
For example:
<pre><%=MyText%></pre>
Better practice for ASP.NET is:
<pre id="myPlaceholder" runat="server"></pre>
Then assign its value from code behind:
myPlaceholder.InnerHtml = MyText;
As for your question "why does it use \r and \n" those are carriage return and line feed characters, aka newline characters - when you have such text:
line 1
line 2
Then code reading it will give: line1\nline2 or line1\r\nline2 depending on how it's stored exactly.
I want to read a text file and load its content to my page. I was trying to read the file with StreamReader and then assign the text to a Label, but the text in the page is just one line. I mean the line in the text file wasn't viewed in the page. What should I do?
Perhaps tackling a symptom rather than the problem itself, you can wrap the text file's contents within a <PRE> tag wherein, unlike most other content in HTML, whitespace is respected.
The textfile uses \n or \r\n for getting new lines (\n is a newline and \r is a carriage return - back in the day of typewritters you had to pull the bar thingy back to the left which is called a carriage return and roll the paper down a line to start on the left side of a newline-). Windows generally uses \r\n (although it depends on the application that created the file) mac's generally use \n.
HTML on the other hand uses the <br/> tag for new lines (if you do a viewsource on your current html output you will see the newlines). So all you need to do is replace \r\n or with . You can do this with:
yourstring = yourstring.Replace("\r\n", "<br/>");
or if you don't know for sure what's used in the file or both \r\n and \n are used you can use
yourstring = yourstring.Replace("\r\n", "<br/>").Replace("\n", "<br/>");
be aware that a string is immutable and thus methods like Replace return a copy of the string that has the replacements made. The original string will stay intact.
Please try this
if (System.IO.File.Exists(Server.MapPath("test.txt")))
{
System.IO.StreamReader StreamReader1 = new
System.IO.StreamReader(Server.MapPath("test.txt"));
lblMyLabel.Text= StreamReader1.ReadToEnd();
StreamReader1.Close();
}
HTML doesn't recognize white-space (line breaks, etc) in your text file. If you want to render the content as HTML, you'll need to convert line-breaks into <br/> tags.
Try something like this:
string path = 'c:\myfile.txt':
lblMyLabel.Text = String.Join('<br/>', File.ReadAllLines(path));