1. Home
  2. Computing & Technology
  3. Visual Basic

Learn ASP.NET 2.0 - A "From the Ground Up" Tutorial
Inline coding: "Classic" ASP

By , About.com Guide

Mar 26 2008

The next step up the ladder toward ASP.NET 2.0 is sometimes called inline coding and represents the kind of coding that was done with "old" or "Classic" ASP. To illustrate this type of coding, let's do approximately the same application using inline code. (By the way, all of these examples were executed in VWD.)

The idea here is that the server interprets what is sometimes referred to as a render block that is embedded in the HTML and then sends HTML back to the browser. So let's change our code to inline code for a "Classic" ASP page:

<html>
   <head>
      <title>
         About VB Hello
      </title>
   </head>
<body>
   <h1><%= "Hello World from About Visual Basic" %></h1>
   <p><%="Inline code embedded in the HTML"%></p>
</body>
</html>

It works, but we have actually gone backwards. What used to be a page that the client computer could create now requires a round-trip to the server and the only thing the server does is send the same text back again. This only makes sense if the server actually does something. To demonstrate the concept, I've added some VB code. It really only results in the same thing again, but it shows how the server executes inline code before sending it back.

<html>
   <head>
      <title>
         About VB Hello
      </title>
   </head>
<body>
   <%
      Dim TextArray(2) As String
         TextArray(0) = "Hello World from "
         TextArray(1) = "About Visual Basic"
      TextArray(2) = _
         "Inline code involving some VB code."
   %>
   <h1><%=TextArray(0) & TextArray(1)%></h1>
   <p><%=TextArray(2)%></p> </body>
</html>

It's worth checking out the actual HTML sent back to the browser by selecting "View Source" in your browser. In this last case, for example, all of the inline code above was converted to:

<h1>Hello World from About Visual Basic</h1>
<p>Inline code involving some VB code.</p>

"Classic" ASP, of course, involved much more than this. Our goal is to make sure you understand what's happening between the client and the server. ----------page 4----------- Code-behind - a better way to code ------------

In the previous page, I noted that simply renaming a standard HTML page as an ASP.NET 2.0 page is not the recommended technique to use. We only did that to illustrate what was happening. Let's look at the way ASP.NET 2.0 actually works now.

Open VWD again if necessary and select File > New Web Site ... from the menu. (You may wonder how to get rid of web sites that you create as tests or by mistake. The easiest way I know of is to delete the folder containing both the Project and the Website from the Visual Studio 2008 folder. If your website is named WebSite1 then delete folders with that name from the Project and Website folders.)

This time, let's do things the VB.NET way! Click the Design button at the bottom of the screen and add a Button and a TextBox control from the ToolBox. As you do this, you'll see that this is a lot like VB.NET 2008 Express, but not exactly. Remember, you're designing a web form now, not a standalone PC application. Everything you do has to be rendered using HTML on a browser so there are a few more limits to what can be done. In general, things have to be placed sequentially on a web page in what is called "flow layout".

Double click in a blank place on the page and the Default.aspx.vb file opens in the editor with the default Sub code already entered for you ... just like VB.NET! Enter ...

Page.Title = "About Visual Basic ASP.NET 2.0"

Notice that Intellisense helps you out ... just like VB.NET. The big difference here is that the Page object doesn't exist in standalone VB.NET projects. This is your web page.

Go back to the Default.aspx design window and click on the Button control to select it and then change the Text property to give it a better look. I used "Display Message" for mine. Then double click on Button to open the code window again and enter ...

TextBox1.Text = "This is ASP.NET 2.0 Coding"

... into the default Click event subroutine ... just like VB.NET again!

And you're done! You can select "Start Without Debugging" or Ctrl-F5 to run the page. The result is shown below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

To nail home the concept, lets examine in detail what just happened.

First, ASP.NET 2.0 notices that your page requires a Build (essentially, the code needs to be compiled) so it does that automatically. One of the big advantages of ASP.NET 2.0 is that pages are automatically compiled when necessary and run as compiled code after that. This makes it very fast compared with other technologies.

Since this is the Load event for the Page, that subroutine is executed. Then ASP.NET 2.0 creates the HTML necessary to display the page including HTML code to display the Button and TextBox. The VWD development server sends it to the default browser on your PC. When you click the Button, the data is posted back to the server for processing and the Click event code for the Button is executed and another HTML page is created and sent to your browser by the server.

As a final exercise to understand what's going on, select View Source for the page in your browser. You'll see the HTML that ASP.NET 2.0 actually created as a result of your code. There's a lot there that you never had to code!

In the next lesson, SQL Server and ASP.NET, we'll add some database coding using the other tool that you downloaded and installed, SQL Server 2005 Express Edition. And we'll learn more about VWD at the same time.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.