Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.Write vs <%= %>

Bearing in mind this is for classic asp

Which is better, all HTML contained within Response.Write Statements or inserting variables into HTML via <%= %>.
Eg

Response.Write "<table>" & vbCrlf Response.Write "<tr>" &vbCrLf Response.Write "<td class=""someClass"">" & someVariable & "</td>" & vbCrLf Response.Write "</tr>" & vbCrLf Response.Write "</table>" & vbCrLf 

VS

<table>   <tr>      <td class="someClass"><%= someVariable %></td>   </tr> </table> 

I am mainly asking from a performance point of view in which will have the least server impact when there multiple variables to insert?

If there are no technical differences what are the arguments for one over the other?

like image 985
Jon P Avatar asked Sep 30 '08 01:09

Jon P


People also ask

What is the difference between response write and response output write?

Response. write() is used to display the normal text and Response. output. write() is used to display the formated text.

What does Response write do in ASP?

Response. Write takes a Unicode string and encodes it to the current Response. CodePage before placing it in the buffer. No such encoding takes place for the static content in an ASP file.

Which method is used by ASP to write output to HTML?

The Response. Write() method is used by ASP to write output to HTML.

What is the simplest way to write an HTML string to the output of an asp net page?

The ASP Write() method is used to display a string as an output on the webpage. It is a predefined method of the Response and Textstream type object. Parameter Values: This method contains the value i.e variant which represents the specified string that you want to display as an Output.


2 Answers

First, The most important factor you should be looking at is ease of maintenance. You could buy a server farm with the money and time you would otherwise waste by having to decipher a messy web site to maintain it.

In any case, it doesn't matter. At the end of the day, all ASP does is just execute a script! The ASP parser takes the page, and transforms <%= expression %> into direct script calls, and every contiguous block of HTML becomes one giant call to Response.Write. The resulting script is cached and reused unless the page changes on disk, which causes the cached script to be recalculated.

Now, too much use of <%= %> leads to the modern version of "spaghetti code": the dreaded "Tag soup". You won't be able to make heads or tails of the logic. On the other hand, too much use of Response.Write means you will never be able to see the page at all until it renders. Use <%= %> when appropriate to get the best of both worlds.

My first rule is to pay attention at the proportion of "variable text" to "static text".

If you have just a few places with variable text to replace, the <%= %> syntax is very compact and readable. However, as the <%= %> start to accumulate, they obscure more and more of the HTML and at the same time the HTML obscures more and more of your logic. As a general rule, once you start taking about loops, you need to stop and switch to Response.Write`.

There aren't many other hard and fast rules. You need to decide for your particular page (or section of the page) which one is more important, or naturally harder to understand, or easier to break: your logic or your HTML? It's usually one or the other (I've seen hundreds of cases of both)

If you logic is more critical, you should weight more towards Response.Write; it will make the logic stand out. If you HTML is more critical, favor <%= %>, which will make the page structure more visible.

Sometimes I've had to write both versions and compare them side-by-side to decide which one is more readable; it's a last resort, but do it while the code is fresh in your mind and you will be glad three months later when you have to make changes.

like image 59
Euro Micelli Avatar answered Oct 16 '22 05:10

Euro Micelli


From a personal preference point of view I prefer the <%= %> method as I feel it provides a better separation variable content from static content.

like image 25
Jon P Avatar answered Oct 16 '22 04:10

Jon P