Understanding ASP.NET

   

The .NET Framework is a programming model for building, deploying, and running XML web services and applications, including Windows and web applications. ASP.NET includes the XML web services and web forms portions of the .NET Framework as seen Figure 1.1.

Figure 1.1. The .NET Framework with the portion that makes up ASP.NET highlighted.
graphics/01fig01.gif

If you look at Figure 1.1, you can see that XML web services and web forms have been encapsulated by a dashed line denoting that they are the portions that are exclusive to ASP.NET. We will be exploring the web forms portion of ASP.NET within this book very heavily and will briefly touch on web services. Truly, web services is a book title unto itself and demands more than I could cover within the scope of this book.

As you can see, there is much more involved with the .NET Framework than just ASP.NET, and I will be investigating the data classes, the base classes, and the common language runtime portions of the Framework throughout this book. Although this may look very confusing and a bit overwhelming, you will see as you progress that these layers or bones of the .NET skeleton make perfect sense. They fit together and give you flexibility, strength, and stability to create, deploy, run, and maintain powerful web applications while avoiding the pitfalls of runtime languages like traditional ASP.

You will see how much easier it is to build applications from the concept of structure instead of relying on the traditional method of runtime-interpreted scripting languages such as traditional ASP.

It's a Whole Different Animal

ASP.NET turns web development from a runtime environment to an object-and event-centric environment. It actually uses full-fledged traditional and new programming languages to produce its results. In actuality, ASP.NET isn't really a language as much as it is a technology.

It is a technology that is driven by a programming language to produce the results you are looking for when you develop applications. It allows you to use languages such as Visual Basic .NET, C# (pronounced C Sharp), and C++ to drive ASP.NET. Don't let this frighten you. You don't need to learn a zillion programming languages to create ASP.NET applications. Just one will do.

"But wait," you're thinking, "I have to learn ASP.NET and Visual Basic .NET or C#. That's got to be a ton of stuff to learn." Fear not. Remember, ASP.NET isn't a language. It's a technology, and you use a language to tell it what to do.

Think of it this way. ASP.NET is like a car. It is potentially very powerful, but by itself it is powerless. It needs fuel to run and direction to guide it. That fuel is the programming language of your choice and the direction is the things you tell it to do through the programming language.

The Differences Between Traditional ASP and ASP.NET

Like I said, ASP.NET is a whole different animal, and the differences between traditional ASP and ASP.NET are many. One of the core differences, as I've mentioned, is that ASP is a runtime language or can also be called an interpreted scripting environment. I've also said that ASP.NET is an object- and event-oriented environment. Let's take a look at this with an example.

Oh No! Not Another "Hello World"!!!

I can hear you now. "Okay, Peter. I thought you said this wasn't going to be a typical programmer's book. But your first example is 'Hello World.' Every programming book starts out with this example."

Please bear with me. I'll try to deviate a bit from the traditional "Hello World" example; but if you can humor me, the simplicity of a "Hello World"-type example will help you to easily understand the differences between the two types of environments.

Below is an example coded in traditional ASP:

 <html>  <head>  <title>No!! Not Hello World!!</title>  </head>  <body bgcolor="#FFFFFF" text="#000000">  <% Response.Write "It was a dark and stormy night with traditional ASP. "%>  </body>  </html> 

The way this code executes is from top to bottom, starting with the opening <html> tag and ending with the closing </html>. The result is shown in Figure 1.2.

Figure 1.2. Traditional ASP intertwines HTML and ASP code and progresses from the first line of code through the last.
graphics/01fig02.gif

As you can see, the results are exactly as expected. And when the page was requested, it ran, and when server reached the line that read

 <% Response.Write "It was a dark and stormy night with traditional ASP. "%> 

it processed the code, and through ASP the line of text was written to the HTML page. Classic ASP is like a snowball that rolls downhill: The code starts at the top and rolls to the bottom and builds as it rolls, executing functions as the code runs. This is what makes it a runtime language.

ASP.NET is more like a car. For you to operate your car, you have interactions with parts of the car. Those parts could be thought of as objects in ASP.NET.

So if you want to get into the car, you grab the door handle and open the door. The door handle is an object and opening the door is an event. Then you may sit down on the seat. Sitting is an event and the seat is an object.

Then you take the key (object), insert (event) it in the ignition (object), turn (event) the key (object), and start (event) the engine (object). Are you beginning to see how the interaction with a car is a series of events and objects and how these are associated with one another?

In the following code you can see a similar example to the traditional ASP page, but written in ASP.NET code using Visual Basic .NET as the scripting language. When you look at this code, try to figure out what the objects and events are (note that throughout this book, code I want to focus on appears in bold):

 <script language="vb" runat=server>  Sub Page_Load(Source As Object,E As EventArgs)      labelText.text = "It was a dark and stormy night in ASP.NET World!"  End Sub  </script>  <html>  <head>  <title>No!! Not Hello World!!</title>  </head>  <body>  <asp:label  runat="server" />  </body>  </html> 

As you can see, the code looks a bit different, and in fact it is quite different from the traditional ASP example. Were you able to pick out the event and the object in this example? If not, don't sweat it before long you will be able to look at even more complex code blocks and figure out what's happening like it's second nature.

Note

You may even be thinking that it appears more complex than traditional ASP. In this particular example there are more lines of code to achieve a similar result than there are in the traditional ASP example, but ASP.NET code reduces the amount of code you write dramatically when you are creating full applications. I have found consistently at least a 30% reduction in code, sometimes reaching as high as 60% reduction in a side-by-side comparison between traditional ASP and ASP.NET


Let's take a look at the code and identify the event and object. The object in this code is an ASP.NET server control called a Label, and its code looks like this:

 <asp:label  runat="server" /> 

You'll learn about ASP.NET server controls in depth in later chapters, so for now let's just say that server controls are objects within the ASP.NET Framework that result in HTML output to the user's browser.

All ASP.NET objects must contain an ID attribute, and this label's ID is "labelText" and is set using . The ID is used to identify an object within ASP.NET, so you may address it within your ASP.NET code.

The runat="server" attribute tells the server that it is a control to be processed at the server to generate HTML and isn't just text to be passed to the browser.

So now I have an object, just like the car, but it can do little without interaction from an event of some sort. When a page is requested, a multitude of events take place, but the one that we are dealing with here is the Page Load event.

 Sub Page_Load(Source As Object,E As EventArgs)      labelText.text = "It was a dark and stormy night in ASP.NET World!"  End Sub 

During the Page Load event, I set the text of the labelText object to the sentence I want displayed when the page is rendered. This can be seen in the line labelText.text = "It was a dark and stormy night in ASP.NET World!" So during the Page Load event the text attribute is set, and during rendering that object is displayed. Look at the result in Figure 1.3.

Figure 1.3. ASP.NET uses the paradigm of events and objects to process its code.
graphics/01fig03.gif

Looks pretty similar to the traditional ASP example, but the way it is achieved through the use of events and objects is completely different.

Just to reinforce the concepts of event- and object-driven programming and to further illustrate how ASP.NET is different from traditional ASP, I thought it would be fun to take the last example, change the text, and move the Page_Load event to the bottom of the page.

 <html>  <head>  <title>Show Event Example</title>  </head>  <body>  <asp:label  runat="server" />  </body>  </html>  <script language="vb" runat=server>  Sub Page_Load(Source As Object,E As EventArgs)      labelText.text = "Notice the event still sets the label's text"  End Sub  </script> 

Now that I've moved the block of code that sets the text of the label to the bottom of the page, look at the results in Figure 1.4.

Figure 1.4. Even when I place the Page_Load event at the end of the code, it still executes when expected because it is an event.
graphics/01fig04.gif

As you can see, the results are exactly the same because the Page_Load event happens before the page is rendered to the user's browser and the label's text is properly set regardless of where the Page_Load block is placed in the page.

Again, ASP.NET code doesn't execute from top to bottom anymore; it is completely driven by events and how you use those events to affect what is delivered in HTML form when the page is rendered.

You will see as we progress that this paradigm of event- and object-oriented application development is a powerful departure from traditional ASP. It is more logical, concise, and productive than its predecessor. If you come to the table with some experience in traditional ASP, I would encourage you to open your mind to the different and more efficient methods that ASP.NET uses. It may take you some time to let go of old thinking and wrap your brain around this programming model, but after you do you'll never go back.


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net