Build Your Own ASP.NET 3.5 Website Using C# & VB (13 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
3.84Mb size Format: txt, pdf, ePub

ASP.NET Basics

41

Disabling View State, Control by Control

View state can also be disabled for particular controls in a page: simply set their

EnableViewState property to false. We’ll see working examples of this in the

following chapters.

Speaking of directives, it’s time to take a closer look at these curious beasts!

Working with Directives

For the most part, ASP.NET pages resemble traditional HTML pages with a few

additions. In essence, just using the
.aspx
extension for an HTML file will ensure

that IIS passes the page to the .NET Framework for processing. However, before you

can work with certain, more advanced features, you’ll need to know how to use

directives.

We talked a little about directives and what they can do earlier in this chapter. You

learned that directives control how a page is created, how a page is cached, help

with bug-fixing, and allow us to import advanced functionality for use within our

code. Three of the most commonly used directives are:

Page

This directive defines page-specific attributes for the ASP.NET page, such as

the language used for server-side code. We’ve already seen this directive in use.

Import

The Import directive makes functionality that’s been defined elsewhere available

in a given page. The following example, for instance, imports functionality from

the System.Web.Mail namespace, which you could use to send email from a

page. Namespaces are simply .NET’s way of keeping all its functionality neatly

organized—we’ll see how they work in
Chapter 3.

<%@ Import Namespace="System.Web.Mail" %>

You’ll become very familiar with this directive as you work through this book.

Licensed to [email protected]

42

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Register

This directive allows you to register a user control for use on your page. We’ll

cover Register in detail in
Chapter 4
, but the directive looks something like this:

<%@ Register TagPrefix="uc" TagName="footer"

Src="footer.ascx" %>

ASP.NET Languages

As we saw in the previous chapter, .NET supports many different languages; in fact,

there’s no limit to the number of languages that could be supported. If you’re used

to writing ASP 2.0 or ASP 3.0, you may think the choice of VBScript or JScript

would be an obvious one. But, with ASP.NET, Microsoft did away with VBScript,

merging it with Visual Basic. ASP.NET’s support for C# is likely to find favor with

developers from other backgrounds. This section will introduce you to both these

new languages, which will be covered in more depth in the next chapter. By the

end of this section, you’ll likely agree that the similarities between the two are astonishing—any differences are minor and, in most cases, easy to figure out. Traditional server technologies are much more constrained in terms of the development languages they offer. For instance, old-style CGI scripts were typically written with Perl or C/C++, JSP uses Java, Coldfusion uses CFML, and PHP is a technology

and a language rolled into one. .NET’s support for many different languages lets

developers choose the ones they prefer. To keep things simple, this book will consider the two most popular: VB and C#. You can choose the language that feels more comfortable to you, or stick with your current favorite if you have one.

Visual Basic

The latest version of Visual Basic is the result of a dramatic overhaul of Microsoft’s

hugely popular Visual Basic language. With the inception of Rapid Application

Development (RAD) in the 1990s, Visual Basic became extremely popular, allowing

in-house teams and software development shops to bang out applications hand over

fist. The latest version of VB has many advantages over older versions, most notably

the fact that it has now became a fully object oriented language. At last, it can call

itself a true programming language that’s on a par with the likes of Java and C++.

Licensed to [email protected]

ASP.NET Basics

43

Despite the changes, VB generally stays close to the structured, legible syntax that

has always made it so easy to read, use, and maintain.

C#

The official line is that Microsoft created C# in an attempt to produce a programming

language that coupled the simplicity of Visual Basic with the power and flexibility

of C++. However, there’s little doubt that its development was at least hurried along

by Microsoft’s legal disputes with Sun. After Microsoft’s treatment (some would

say abuse) of Sun’s Java programming language, Microsoft was forced to stop developing its own version of Java, and instead developed C# and another language, which it calls J#. We’re not going to worry about J# here, as C# is preferable. It’s

easy to read, use, and maintain, because it does away with much of the confusing

syntax for which C++ became infamous.

Summary

In this chapter, we started out by introducing key aspects of an ASP.NET page including directives, code declaration blocks, code render blocks, includes, comments, and controls. As the chapter progressed, we took a closer look at the two most

popular languages that ASP.NET supports, and which we’ll use throughout this

book.

In the next chapter, we’ll create a few more ASP.NET pages to demonstrate form

processing techniques and programming basics, before we turn our attention to the

topic of object oriented programming for the Web.

Licensed to [email protected]

Licensed to [email protected]

Chapter3

VB and C# Programming Basics

As you learned at the end of the last chapter, one of the great things about using

ASP.NET is that we can pick and choose which of the various .NET languages we

like. In this chapter, we’ll look at the key programming principles that will underpin

our use of Visual Basic and C#. We’ll start by discussing some of the fundamental

concepts of programming ASP.NET web applications using these two languages.

We’ll explore programming fundamentals such as variables, arrays, functions, operators, conditionals, loops, and events, and work through a quick introduction to object oriented programming (OOP). Next, we’ll dive into namespaces and address

the topic of classes—seeing how they’re exposed through namespaces, and which

ones you’ll use most often.

The final sections of the chapter cover some of the ideas underlying modern, effective

ASP.NET design, including code-behind and the value it provides by helping us

separate code from presentation. We finish with an examination of how object oriented programming techniques impact upon the ASP.NET developer. Licensed to [email protected]

46

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Programming Basics

One of the building blocks of an ASP.NET page is the application logic: the actual

programming code that allows the page to function. To get anywhere with ASP.NET,

you need to grasp the concept of
events
. Most ASP.NET pages will contain controls

such as text boxes, checkboxes, and lists. Each of these controls allows the user to

interact with the application in some way: checking checkboxes, scrolling through

lists, selecting list items, and so on. Whenever one of these actions is performed,

the control will raise an event. It’s by handling these events within our code that

we get ASP.NET pages to do what we want.

For example, imagine that a user clicks a button on an ASP.NET page. That button

(or, more specifically, the ASP.NET Button control) raises an event (in this case, it

will be the Click event). A method called an
event handler
executes automatically

when an event is raised—in this case, the event handler code performs a specific

action for that button. For instance, the Click event handler could save form data

to a file, or retrieve requested information from a database. Events really are the key

to ASP.NET programming, which is why we’ll start this chapter by taking a closer

look at them.

It wouldn’t be practical, or even necessary, to cover
all
aspects of VB and C# in this book, so we’re going to discuss enough to get you started, and complete this chapter’s

projects and samples using both languages. Moreover, the programming concepts

you’ll learn here will be more than adequate to complete the great majority of dayto-day web development tasks using ASP.NET.
Control Events and Subroutines

As I just mentioned, an event (sometimes more than one) is raised, and handler

code is called, in response to a specific action on a particular control. For instance,

the code below creates a server-side button and label. Note the use of the OnClick

attribute on the Button control. If you want to test the code, save the file in the

LearningASP
directory you’ve been using for the other examples. Here’s the VB version: Licensed to [email protected]

VB and C# Programming Basics

47

Visual Basic

LearningASP\VB\ClickEvent.aspx

<%@ Page Language="VB" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Click the Button





OnClick="button_Click"
Text="Click Me" />






Here’s an excerpt from the C# version:

C#

LearningASP\CS\ClickEvent.aspx
(excerpt)

<%@ Page Language="C#" %>




Licensed to [email protected]

48

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

The HTML and ASP.NET elements in the page are the same for both the C# and VB

versions of the code, so I’ve shown only the differences in the code listing for the

C# version above. This approach will be used for the other examples in this chapter,

too. The complete C# version can be found in the code archive.

When the button’s clicked, it raises the Click event, and ASP.NET checks the button’s OnClick attribute to find the name of the handler subroutine for that event. In the code above, we instruct ASP.NET to call the button_Click routine. You’ll

see the code for the button_Click routine within the

C#

LearningASP\CS\ClickEvent.aspx
(excerpt)


This code simply sets a message to display on the Label element that we declared

with the button. So, when this page is run, and users click the button, they’ll see

the message “Hello World” appear next to it, as shown in
Figure 3.1.

Licensed to [email protected]

VB and C# Programming Basics

49

Figure 3.1. Handling the Click event

By now, you’ll be starting to come to grips with the idea of events, and the ways in

which they’re used to call particular subroutines. In fact, there are many events that

your controls can use, though some of them are found only on certain controls.

Here’s the complete set of attributes that the Button control supports for handling

events:

OnClick

As we’ve seen, the subroutine indicated by this attribute is called for the Click

event, which occurs when the user clicks the button.

OnCommand

Other books

Broken Survivor by Jennifer Labelle
Poor Tom Is Cold by Maureen Jennings
Finding Sophie by Irene N.Watts
The Satyr's Head: Tales of Terror by Campbell, Ramsey, Lumley, Brian, Riley, David A.
Double Dexter by Jeff Lindsay
Rhythm of the Spheres by Abraham Merritt
First and Last by Rachael Duncan