Page

3.2.2- Output to the User

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 03. Basic ASP Techniques.

Not tagged.
<< 3.2.1- Capturing and Storing the InformationChapter33.2.3- A Simple Business Example >>

Output to the User

If we want to display text on a page, there are two techniques available to us. The first technique is simply to type the text we want into a section of the HTML code, outside of the ASP <%%> delimiters. For example, if we wanted the words Autumn Sweaters to appear on our page, we could use the following construction:

 

<HTML>

...

Autumn Sweaters

...

</HTML>

 

The second technique is to instruct ASP to display the text on the screen – and to do that we use the Response.Write syntax. The Response.Write command instructs ASP's Response object to write the requested text to the browser. For example, if we want the words Autumn Sweaters to appear as before, but this time from within a section of ASP code, we could use the following construction:

 

<HTML>

...

<%

Response.Write "Autumn Sweaters"

%>

...

</HTML>

 

You can, of course, use the first technique on a page that contains ASP code, provided you make sure that you do not enter the text within an ASP code section, or within <SCRIPT> blocks. If we had accidentally forgotten that we were working within the ASP delimiters (the <% and %> tags) and not included the keywords Response.Write and the quotes, we would receive an error. That 's because ASP would have tried to find a command with the name Autumn with a parameter Sweaters – since no command exists with this name, the user who requested the page would have an error page sent back to them.

 

On the other hand, if we had typed in Response.Write "Autumn Sweaters" from outside of the ASP code block, we would have a page with the text Response.Write "Autumn Sweaters" appearing on it.

 

To summarize, these techniques are correctly use in the following ways:

 

Region of code on page:

Type in

Result in Browser

HTML

Autumn Sweaters

Autumn Sweaters

ASP

<%
Response.Write
"Autumn Sweaters"
%>

Autumn Sweaters

And if you use them like this, you won't get the results you were looking for:

 

Region of code on page:

Type in

Result in Browser

HTML

Response.Write "Autumn Sweaters"

Response.Write "Autumn Sweaters"

ASP

Autumn Sweaters

Error

 

So we can see that Response.Write is great for writing text into the page. But there's more: we can also use Response.Write to write HTML tags into the page. For example, to have ASP add a horizontal line to the page, we can use Response.Write to put the <HR> tag into the HTML page as follows:

 

<%

Response.Write "<HR>"

%>

Outputting the Values of Variables

ASP also works well for putting the contents of a variable into a page's source HTML. Let's say we have asked the user in a form for the name of his item of interest. We could write the name of that item onto the page by using the following code:

 

<%

Dim strItem

strItem = Request.Form("ItemChoice")

Response.Write strItem

%>

 

The first line indicates the start of the ASP code. Then we create a variable called strItem. The third line takes the data that the user typed into the form field called ItemChoice, and it copies that data into the variable called strItem. This line makes the user's input available for ASP. The fourth line writes the contents of strItem onto the HTML page and then the fifth line closes off the ASP section of code.

 

It's important not to underestimate the power of what we have just done! With this simple technique, the text that is written on this page can potentially be different for each different user. With static HTML, we can only send the exact same page to every visitor; but with ASP techniques like this we can customize our pages to reflect the exact needs of each user.

Note that Response.Write uses a slightly different syntax for variables and for text or tags. When using Response.Write for variables, the variable name should not be in quotes; when using Response.Write for text and tags, the text and tags should be in quotes.

Mixing Lines of HTML and ASP Output

As we said at the beginning of this section, there are two ways to write output to the browser – the pure HTML technique and the ASP Response.Write technique – and we can use these two techniques interchangeably within the code of our ASP pages. For example:

 

<P>Next week's featured item:

<%

Dim strItem

strItem = Request.Form("ItemChoice")

Response.Write strItem

%>

</P>

 

In the above example the first line consists of straight text and HTML tags, just as if you had never heard of ASP. The second line starts an ASP block, which writes some more text. (Inside the block we first create the strItem variable; then we populate our new variable with the user's information; then we direct ASP to write the contents of strItem onto the HTML page.) So if the user had typed the phrase Autumn Sweaters into the ItemChoice field and submitted that value, the result of the above code would be a page in the browser as shown below.

 

Chapter3_image009

 

This intermingling of HTML and ASP-written text applies to the tags as well. If we wanted to make some of the words in our display bold (using the <B> and </B> tags), we could insert the tag from HTML like this:

 

<P>Next week's featured item: <B>

<%

Dim strItem

strItem = Request.Form("ItemChoice")

Response.Write strItem

%>

</B></P>

In the above example the paragraph starts with some tags and text, including an opening <B> tag, before the ASP code begins. ASP writes the contents of the strItem variable in bold type. Then ASP finishes and the HTML page adds the closing </B> tag. Once the ASP has calculated the value of strItem (assuming the user typed in the value Autumn Sweaters), the result is the same as if you had just written straight HTML as follows:

 

<P>Next week's featured item: <B>Autumn Sweaters</B></P>

 

Alternatively, we could have written those <B> and </B> tags to the page from within the ASP code, like this:

 

<P>Next week's featured item:

<%

Dim strItem

strItem = Request.Form("ItemChoice")

Response.Write "<B>"

Response.Write strItem

Response.Write "</B>"

%>

</P>

 

This time, we've removed the <B> and </B> tags from the pure-HTML lines. Instead, we've got three Response.Write statements, to write three different things to the browser. The ASP Response.Write command doesn't care if it is writing text, tags or a mixture. You can put text plus one or more tags in a single Response.Write (although this wouldn't look too pleasing to the eye):

 

<P>Next week's featured item:

<%

Dim strItem

strItem = Request.Form("ItemChoice")

Response.Write "<B>"

Response.Write strItem

Response.Write "</B>. <I>Suits you, Sir!</I>"

%>

</P>

 

Chapter3_image010

Of course, the power that ASP's Response.Write brings to our pages is that it allows us to write the values of variables and functions to the page. The exact display shown in the page happens because the user typed the string Autumn Sweaters into a form, and submitted that value; and it was then captured by the new page's Request.Form collection and used to write the new page. We simply can't do that using plain HTML.

 

If the user had typed in Fleecy Trousers, then the resulting page above would have been different:

 

Chapter3_image011

 

OK, this page isn't much different to the previous one – but imagine what we can do once we've got a few variables together, and made some decisions dynamically based on the values of those variables. We'll soon have some exciting, complex, dynamic pages that will get the users coming back for more.

A Shortcut for Response.Write

Now let us finish with a shortcut. We're likely to use the Response.Write many times in order to write the values of all our variables to the page – so for that very reason, ASP provides a special shorthand form of Response.Write. We can use the syntax <%=strName%> you can write the contents of the variable strName onto the page.

 

For example, suppose a customer is using a web site to buy a rectangular piece of carpet, and they have submitted values for the length and width of the piece of carpet they want to buy. In the page that handles these values, we might handle the inputted data like this:

 

<%

intLength = Request.Form("CarpetLength")

intWidth = Request.Form("CarpetWidth")

%>

Your piece of carpet will have length

<% Response.Write intLength %>

and width

<% Response.Write intWidth %>

The two Response.Write calls are rather clumsy, but we can rewrite this code using the shortcut instead, which makes the code much easier to read:

 

<%

intLength = Request.Form("CarpetLength")

intWidth = Request.Form("CarpetWidth")

%>

Your piece of carpet will have length <%=intLength %> and width <%=intWidth %>

 

Note that you can only use this shortcut to abbreviate a Response.Write, and it doesn't work if you mix it with other ASP commands in the same ASP code block. For example, in the following sample the highlighted lines will fail:

 

<%

intLength = Request.Form("CarpetLength")

intWidth = Request.Form("CarpetWidth")

Response.Write "Your piece of carpet will have length "

=intLength ' this line is illegal

Response.Write " and width "

=intWidth ' this line is illegal

%>

 

So, in this section we have talked about how to direct ASP to write characters to the HTML page before it goes out to the user. Here are the take-home points:

 

  • These characters can be of three types: text, HTML tags, or the contents of a variable
  • The command to write characters to the HTML page from within ASP code is Response.Write
  • The syntax for text is: Response.Write "text goes here"
  • The syntax for variables is: Response.Write strName
  • We also covered a shortcut to put the contents of a variable into a line of HTML text using the equals sign, straight after the opening ASP delimiter: <%=strName%>

 

Let's now improve the confirmation page that we return after asking the user for their department in our example.

Try It Out – Registration of Department Reply

In our form SpringRetreatDepartForm.asp, the <FORM> tag has the attribute ACTION="SpringRetreatNotice.asp". But the reply wasn't very informative: it just said Thank you for registering your department. It didn't even tell the user whether they had successfully registered the correct department name.

 

So let's amend this second page to show text that not only confirms the visitor's registration, but also confirms which department they have registered. Keep in mind that it won't be until later in the book that you will learn how to actually make a registration in a database. But for now, we will just focus on returning a message to the user.

1.    This solution needs you to change and add the highlighted lines to SpringRetreatNotice.asp.

 

<HTML>

<HEAD>

<TITLE>Spring Retreat Notice</TITLE>

</HEAD>

 

<BODY>

<H1>Spring Retreat</H1>

<H2>Thank you for registering as a member of the

<%

Dim strDepartment

strDepartment = Request.Form("Department")

Response.Write strDepartment

%>

department.</H2>

</BODY>

</HTML>

 

2.    Save the page with the same name as previously.

3.    Now, open SpringRetreatDepartForm.asp in the browser; fill in the text box and click the button marked Click here to send this information. This will cause our revised version of SpringRetreatNotice.asp to appear on the browser. If you typed the word Sales into the input field and submitted that, then you'll see the following confirmation page:

Chapter3_image012

How It Works

This code starts with the same initial information as we used in the first Try-It-Out for forms.

 

<HTML>

<HEAD>

<TITLE>Spring Retreat Notice</TITLE>

</HEAD>

 

<BODY>

<H1>Spring Retreat</H1>

<H2>Thank you for registering as a member of the

 

But at this point we kick in ASP with a <%, and our first ASP statement creates a variable named strDepartment. ASP populates this variable with the data sent from the browser and labeled as Department – which, in the case of the screenshot above, is the word Sales. The third ASP statement writes the contents of the variable strDepartment onto the page. Then the ASP code section is closed:

 

<%

Dim strDepartment

strDepartment = Request.Form("Department")

Response.Write strDepartment

%>

 

In order to make the page look neater, we've add one line of simple HTML text to finish off the sentence. Then we close up the body and the page:

 

department.</H2>

</BODY>

</HTML>

 

OK, now our SpringRetreatNotice.asp page is a bit more useful – because it tells the user some useful information, based on what action they took in the first page.

 

What about our jacket registration form? That page sends three fields of data to the server. Now, since we now know how to put that information into variables, let's produce a more functional confirmation page that reads this information back to the user.

Try It Out – Jacket Order Confirmation

1.    Open the form SpringRetreatJacketConfirmation.asp once again, and replace all of the code between the <BODY> tags with the following:

<BODY>

<%

Dim strGender, strSize, strColor

strGender = Request.Form("Gender")

strSize = Request.Form("Size")

strColor = Request.Form("Color")

%>

 

<H1>Company Spring Retreat</H1>

<H3>Jacket Order: Confirmation</H3>

For you, we will order a jacket in

<%Response.Write strColor%>

and size

<%Response.Write strSize%>

for a

<%Response.Write strGender%>

employee.

</BODY>

 

2.    Save your changes to that file. Open SpringRetreatJacketForm.asp in a browser, fill in the details male, L and Argent into the relevant boxes and click on Save my Preferences to submit this to the server. Then you will see the following:

Chapter3_image013

How It Works

We know how the HTML form works, so let's just look at how our revised SpringRetreatJacketForm.asp code handles the submitted data.

 

The first few lines of the page's body contain a block of ASP logic. First, we create the three variables, and assign to these variables the values that were entered by the user in the previous page:

 

<BODY>

<%

Dim strGender, strSize, strColor

strGender = Request.Form("Gender")

strSize = Request.Form("Size")

strColor = Request.Form("Color")

%>

Then we shift out of ASP and into HTML, to put some text on the page:

 

<H1>Company Spring Retreat</H1>

<H3>Jacket Order: Confirmation</H3>

For you, we will order a jacket in

 

The next line instructs ASP to write the contents of the variable named strColor on the HTML page. ASP respects the position of this command within the surrounding HTML and text, and writes the contents of strColor to just the right place in the page:

 

<%Response.Write strColor%>

 

Now we shift out of ASP again, to write a little more pure text. Then we shift back into ASP, to write the user's input for Size into just the right point in the page:

 

and size

<%Response.Write strSize%>

 

Then some more pure text, one last variable value (in an ASP block) and one more line of pure text to finish of the output:

 

for a

<%Response.Write strGender%>

employee.

Alternative Solutions to Our Problem

Here's a second solution that uses less shifting between HTML and ASP, but more Response.Write calls. We can write both the boiler-plate text and the contents of the variables using Response.Write as follows:

 

<BODY>

<%

Dim strGender, strSize, strColor

strGender = Request.Form("gender")

strSize = Request.Form("Size")

strColor = Request.Form("Color")

Response.Write "<H1>Company Spring Retreat</H1>"

Response.Write "<H3>Jacket Order: Confirmation</H3>"

Response.Write "For you, we will order a jacket in "

Response.Write strColor

Response.Write " and size "

Response.Write strSize

Response.Write " for a "

Response.Write strGender

Response.Write " employee."

%>

</BODY>

There's less shifting into and out of ASP here – the whole body section is ASP code. Each block of ASP code, enclosed in a pair of <%%> delimiters, requires another call to the ASP DLL. This means that the more times you use <%%>, the longer your code will take to execute. Holding all our code within a single block of ASP can give you a noticeable run-time speed increase over using many blocks of ASP. There are more Response.Write statements, which can make the code look a little bulky, but the performance issue is am important one to consider.

 

We could reduce the number of Response.Write calls in the above sample by using a technique called string concatenation. This involves fixing two or more smaller strings together to make one big string. After concatenating all the small strings into one big string, we'd just call Response.Write once – to write the big string to the browser. There's more about string concatenation in Chapter 4 .

 

A third solution is easier to read, but uses more ASP blocks. In this case, we'll use the Response.Write shortcut when writing the output to the browser:

 

<BODY>

<%

Dim strGender, strSize, strColor

strGender = Request.Form("gender")

strSize = Request.Form("Size")

strColor = Request.Form("Color")

%>

<H1>Company Spring Retreat</H1>

<H3>Jacket Order: Confirmation</H3>

For you, we will order a jacket in <%=strColor%> and size <%=strSize%> for a <%=strGender%> employee.

</BODY>

 

All three of these solutions produce the result on the browser. I recommend the second of the three solutions, because it is the fastest to execute – it requires fewer trips to the ASP DLL.

<< 3.2.1- Capturing and Storing the InformationChapter33.2.3- A Simple Business Example >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |