I was doing some poking around with ASP.NET and I stumbled into some weird whitespace behavior with the rendered output. It seems quite hard to properly control indentation and newlines, especially when using loops. I made a sample to illustrate my issue:
<%@ Page Language="C#" %>
<%
string[] StringList = new string[]{"Stack", "OverFlow", "Rocks", "My", "Socks", "Of"};
%>
*** No whitespace before each word... ***
<% for(int word=0;word<StringList.Length;word++){ %>
<%= StringList[word] %>
<% } %>
*** No whitespace and no newline before each word... ***
<% for(int word=0;word<StringList.Length;word++){ %>
<%= StringList[word] %>
<% } %>
*** No whitespace and a newline after each word... ***
<% for(int word=0;word<StringList.Length;word++){ %>
<%= StringList[word] %>
<% } %>
*** Whitespace before each word...and some "free" newline before each word ***
<% for(int word=0;word<StringList.Length;word++){ %>
_<%= StringList[word] %>
<% } %>
*** Whitespace and a newline before each word...and some "free" newline before each word ***
<% for(int word=0;word<StringList.Length;word++){ %>
_<%= StringList[word] %>
<% } %>
The output that was generated for this code looks like this (in source code):
*** No whitespace before each word... ***
Stack
OverFlow
Rocks
My
Socks
Of
*** No whitespace and no newline before each word... ***
Stack
OverFlow
Rocks
My
Socks
Of
*** No whitespace and a newline after each word... ***
Stack
OverFlow
Rocks
My
Socks
Of
*** Whitespace before each word...and some "free" newline before each word ***
_Stack
_OverFlow
_Rocks
_My
_Socks
_Of
*** Whitespace and a newline before each word...and some "free" newline before each word ***
_Stack
_OverFlow
_Rocks
_My
_Socks
_Of
Is it possible to have some control over the whitespace handling?
An additional note based on the ultra fast responders (thx for that btw)! I don't want to generate HTML. I want to use ASP.NET as a template engine. Which is really nice and fast, but it seems to lack whitespace control. Hence my question.
Thank you alot!
ASP.NET produces HTML. Learn the basics of HTML and CSS styles to understand white-space treatment and rendering.
What was produced in plain-text format does not mean it will be rendered like that by a browser. You have to use HTML markup and CSS styling to format the output.
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