Reputation: 2138
Please take a look at the following ASP.NET code:
<%@ Page Language="C#" %>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<p><%Response.Write("This is sentence 1.");%> <%Response.Write("This is sentence 2.");%></p>
</body>
</html>
I expected it to build a short paragraph by joining two strings together, with a white space between them (please note the white char between <%Response.Write("This is sentence 1.");%>
and <%Response.Write("This is sentence 2.");%>
). However, the output HTML I get from IIS 7.5 is:
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<p>This is sentence 1.This is sentence 2.</p>
</body>
</html>
Which contains no white space between both sentences. Interestingly, if I place the white space inside the second sentence:
<%@ Page Language="C#" %>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<p><%Response.Write("This is sentence 1.");%><%Response.Write(" This is sentence 2.");%></p>
</body>
</html>
Then it is carried on to the HTML. But I would prefer the white spaces to be in the code building the composition, not in the data on which it works, since I do not know, by the time I write the individual sentences, which ones will go into the paragraph or in what order.
Is this the expected behaviour, or am I doing something wrong?
UPDATE:
VDWWD points out an interesting remark; if I use <%="..."%>
instead of <%Response.Write("...");%>
the whitespace is indeed carried on to the HTML. But that makes me scratch my head even more, because this works on my simplified test case posted above, but not on my actual use case which looks more like this:
...
<p><%=TextoWeb("Ponencias", "QuieresSubirTuPonencia?")%><%
var InicioPonencias = Sesión.ElementoTimelinePorNombre("INICIO PONENCIAS");
if (DateTime.Today < InicioPonencias.Fecha) {
%> <%=TextoWeb("Ponencias", "TextoAntesAperturaPonencias", InicioPonencias.Fecha.ToLongDateHtml(Sesión.Cultura))%><%
}
%></p>
...
Please excuse the Spanish and the non-standard extensions. Function TextoWeb
retrieves some localised text by category and name according to the language of the page being built, Sesión.ElementoTimelinePorNombre
retrieves some timeline item by name and .ToLongDateHtml(System.Globalization.CultureInfo)
does some language-specific high level formatting of the dates to add things like ordinal indicators. The purpose of this specific piece of code is adding a sentence to an existing paragraph, but only if the current date is earlier than a certain date.
The thing is that I am using <%=(...)%>
instead of <%Response.Write(...);%>
but the white space is not being carried on to the HTML.
Upvotes: 1
Views: 757
Reputation: 2415
You can use a HTML entity which is  
for a regular space, or the more memorable
for a non-breaking space if that is desired.
I don't know the exact mechanism behind the stripping, but most likely it has something to do with the order in which cshtml is compiled. Stripping the whitespace from an empty tag seen as <p> </p>
by the engine before being populated would be expected behaviour.
Upvotes: 2
Reputation: 35564
Do not use Response.Write. Then the space is there.
<%= "This is sentence 1." %> <%= "This is sentence 2." %>
Update
You probably don't get a space because you have split the <% %>
between inline code. The compiler makes it a single line. Try to do the if statements in code behind for much cleaner aspx. Or you a ternary opertator, that way there is also a space.
<p><%=TextoWeb("Ponencias", "QuieresSubirTuPonencia?YYY")%> <%= DateTime.Today < DateTime.Now ? TextoWeb("xxxPonencias", "QuieresSubirTuPonencia?YYY") : "" %></p>
Upvotes: 1