Space Management in ASPX / ASP.NET

I made several attempts with ASP.NET, and I came across some weird behavior of spaces with visualized output. Apparently, it’s quite difficult to properly control indentation and newlines, especially when using loops. I made a sample to illustrate my problem:

<%@ 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 result that was generated for this code is as follows (in the 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 handling of spaces?

Extra note based on ultrafast answers (thanks for that by the way)! I do not want to generate HTML. I want to use ASP.NET as a template engine. Which is really nice and fast, but it seems he lacks control over the spaces. Hence my question.

Thanks a lot!

+3
1

ASP.NET HTML. HTML CSS, .

, . HTML CSS.

+2

All Articles