Response.Write in ASP

Changes are if you are using ASP you will sometime want to write in something into a page without going back into HTML. You can always open an IF then close the ASP script write in your HTML then open the ASP script up again and END IF. However this is not always practical.

When it’s needed

Some WYSIWYG editors will display little ASP icons where ASP scripts are and if you are using one page to house several pages of content then you will want it all to be inside the ASP script so that it doesn’t mess up and stretch the layout of your page.

However if you are opening and closing ASP scripts with HTML in between, all the HTML will display in between all these scripts and will be fully visible on your page rather than being contained in the little ASP script icon.

Another instance would be when you want your HTML content to be in a variable. Say if you wanted to use the same block of HTML script in two places or repeatedly say for a newsletter script then it would be easy if all the HTML was in a variable you could just include anywhere in your ASP script.

Being ASP ready

Most HTML tags will go in the basic format for Response Write in ASP fine as there are no limitations as its al contained within markers to show what is ASP script and what is the content you are setting as a variable. For example:

Response.Write("all the code goes here and its all contained in this area nicely")

The problem is that often in HTML, the quote mark is used. But using one of these ” in a response write ASP command will close the Response Write area and code will be left out and start causing all sorts of errors. For example:

Response.Write("<img src="somepath_is_outside_the_quotes"")

Quoting the problem

How do you contain these problems then? By removing or even adding quotes. The first option is to simply remove all the quotes or as many as you can. This means that it isn’t formatted correctly and W3C won’t be happy with you but they never are anyway ;). It will still work in the user’s browser at any rate.

<img src=somefile.gif width=100 height=200 border=0 />

This code appears fine, and it would display fine. This can then be put between the quotes in a Response Write tag in ASP. Problems do not end there though – what happens if your need spaces in a variable. There you couldn’t use the above tactic.

Spaces and doubles

Take a look at the following code. There may be better examples but this one works fine. Its an image with an ALT tag with some keywords in it.

<img src=somefile.gif border=0 alt="some keywords go here" />

There is a problem. The ALT tag has spaces in, so after some, the browser may think that everything beyond there is something else. And with good reason as we could replace keywords go here with width=50 and we wouldn’t want that to be part of the ALT tag.

The solution it to use double quotes.

<img src=somefile.gif border=0 alt=""some keywords go here"" />

This keeps the ASP code correct and adds a quote into the HTML when it is sent to the browser. It’s a bit longer and more code that usual but you can easily change all the ” to “” with a quick find and replace.