How to display your code

Say you want to write an article about ASP. How do you put ASP code in an ASP page and just display it? How do you tell the server you just want to display the code and not execute it? Here is my solution:

<%
whichfile=server.mappath("snippet.asp")

Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile(whichfile, 1, False)

do while not thisfile.AtEndOfStream
thisline=thisfile.readline
response.write server.htmlencode(thisline) & "<br>"
loop

thisfile.Close
set thisfile=nothing
set fs=nothing
%>


What I do is:
1. Use the File System Object to read a line of the ASP page.
2. Check if we are at the end of the page
3. If not use server.htmlencode to write that line to the browser

That's it!!!

Here is the ASP file: snippet.asp



Come learn HTML or Classic ASP in Honolulu, Hawaii!

HOME | SQL Tutorials | XML Tutorials | OKIWEB | EMAIL ME | ASP Alliance