efore we can dive in and begin building Web pages with ASP.NET, I want to introduce a few topics and new terminology.
.NET Framework
Microsoft designed classes in the .NET Framework to accommodate your current programming needs and beyond. It contains classes for many activities including manipulating text, working with database data, and working with the file system. The .NET Framework also contains classes that represent the basic data types including integers, bytes, strings, and arrays. You should learn one of the .NET languages, but it is vital that you learn about the .NET Framework. Do you have to know how to use every class in the Framework before you can become an effective .NET developer? No, but there are quite a few classes you need to learn about before you will make any substantial headway. When it comes to developing in .NET, the Framework is everything.
Fast Facts |
ASP.NET is actually built into the .NET Framework. You technically don’t even need Visual Studio .NET to write ASP.NET code. |
|
Namespaces
As you can probably imagine, the entire .NET Framework is huge. It is comprised of thousands of classes, or as astronomer Carl Sagan might say, “billions and billions!” Management and classification of this enormous list of classes requires an organizational mechanism. The organizational mechanism that manages the .NET Framework is called a namespace. Simply put, a namespace is a logical grouping of classes. For example, the System.Web namespace stores all of the classes that relate to working with browser-server communication.
The .NET Framework organizes namespaces into a hierarchical tree structure. The System namespace resides at the root of this tree. It contains all of the classes for working with the base data types, random numbers, dates, directories, and other system resources.
To uniquely identify a class in the .NET Framework, you reference the class by its full namespace and class name. For example, to refer to the File class contained in the System.IO namespace you would use:
System.IO.File
Table 1 shows a partial list of standard namespaces.
Namespace | Description |
---|---|
System | The root namespace for the entire .NET Framework. Contains the base types and other useful classes. |
System.Collections | Contains classes for working with the standard collection types. |
System.Configuration | Contains classes for working with Web configuration files (web.config). |
System.Data | Contains classes for interaction with data sources (ADO.NET). |
System.DirectoryServices | Contains classes for accessing Active Directory Services. |
System.IO | Contains classes for reading and writing files. |
System.Text | Contains classes for working with strings. |
System.Xml | Contains classes that process XML data. |
.NET Languages
.NET supports a number of languages. By default, Visual Studio .NET installs Visual C# .NET (usually abbreviated as C# and pronounced c-sharp), Visual Basic .NET, and Visual C++ .NET. The two languages most commonly used in .NET are Visual Basic .NET and C#. Other languages that have been ported over to .NET include COBOL, Eiffel, and PERL. I’ll use Visual Basic .NET throughout this article.
File Extensions
As is the case with just about every new development environment, you’re going to need to be aware of a few new file extensions in Visual Studio .NET.
Table 2 lists common file extensions that ASP.NET uses.
Extension | Description |
---|---|
.aspx | ASP.NET page file |
.ascx | ASP.NET User Control file |
.asmx | Web service file |
.aspx.vb | Visual Basic .NET code file for an ASP.NET page |
.aspx.cs | C# code file for an ASP.NET page |
.sln | Visual Studio .NET Solution file |
Set Up and Installation
In order to run ASP.NET Web Forms you need to have Internet Information Server (IIS) and the .NET Framework Software Development Kit (SDK) installed on your machine (installing IIS is beyond the scope of this article).
Notice I didn’t say you need Visual Studio .NET installed on your machine to build and run ASP.NET Web Forms. Just like standard HTML, you can create your ASP.NET Web Forms in any text editor. However, we’ll use Visual Studio .NET as the development environment.
Your First ASP.NET Project
Now you’re ready to get your feet wet. Launch Visual Studio .NET and let’s get started.
To create a new ASP.NET project in the Visual Studio .NET IDE, select File | New | Project, and then select ASP.NET Web Application from the Templates. Name the project LearnASPNet (see Figure 1), click OK, and Visual Studio will build the project files. The Solution Explorer will contain your newly created project and a Web Form named Webform1.aspx (see Figure 2).
Figure 1: Create new projects in Visual Studio .NET using the New Project dialog box. There is a template for each type of .NET application. |
Figure 2: The Solution Explorer contains all of the items referenced in the application. |
The first thing to do is rename Webform1.aspx to MyFirstWebForm.aspx. Right-click on Webform1.aspx and select Rename. Then, right-click on MyFirstWebForm.aspx again and select Set As Start Page. This ensures that MyFirstWebForm.aspx will be the Web Form initially displayed when you run the project.
Let’s launch this Web Form just to make sure you configured your machine correctly. Click the blue triangle in the toolbar (see Figure 3), select Debug | Start, or press F5 to launch the form.
Figure 3: The Visual Studio .NET toolbar provides a convenient way to open and save projects, cut, copy, and paste elements, and run your projects. |
If you set up your machine correctly you should see a blank Web page (see Figure 4). Congratulations, you’ve just created and run your first ASP.NET Web Form! Close the browser and return it to the Visual Studio .NET IDE.
Figure 4: Running the project also compiles the project and launches the browser loaded with the start page, MyFirstWebForm.aspx. |
One of the first things to note about your newly-created Web page is the text in the middle regarding the layout mode of the page. There are two options for layout mode: GridLayout and FlowLayout. You specify the layout mode used by the page on the General tab of the DOCUMENT Property Pages dialog box. Right-click on the page and select Properties to display the DOCUMENT Property Pages dialog box (see Figure 5).
|
You’re probably wondering, what’s the difference between the two layout modes? GridLayout provides absolute positioning for controls placed on the page. Developers that have their roots in rich-client development environments like Visual Basic will find it easier to develop their pages using absolute positioning, because they can place items exactly where they want them. On the other hand, FlowLayout positions items down the page like traditional HTML. Experienced Web developers favor this approach because it results in pages that are compatible with a wider range of browsers.
Figure 5: Use the DOCUMENT Property Pages dialog box to configure settings of a page. Commonly used settings include Page title, Default scripting language, Page Layout, and Keywords. |
Which layout should you use? Flowlayout is the safer route, however, absolute positioning is appropriate for an intranet application when you are sure all of your users will have a compatible browser.
While you still have the Property Pages dialog box open, go ahead and change the Page Title to My First Web Form. Click OK to close the dialog box.
Adding Controls
We will examine a few controls shortly, but first you need to learn how to add a control to a form. The Toolbox is located on the left side of the screen. Select the Web Forms controls section and double-click the TextBox control. You should now see a text box on your Web Form. Right-click the text box and select Properties from the shortcut menu. Change the ID property to txtName (see Figure 6). Next, add a button control. Name the button cmdSubmit and set its Text property to Away I Go!
|
ASP.NET Controls When you create an ASP.NET page, you have the option of using four different types of controls. You can use the HTML Controls, which are server-side versions of the standard HTML tags; you can use the Web Controls, which are a more flexible and object-oriented set of controls; you can use the Validation Controls, which provide automatic data validation; and you can use User Controls, which are developer-created controls. Although you can use a combination of standard HTML tags and Web Controls, HTML gets sent back to the client’s browser. Microsoft designed the ASP.NET controls to help make creating Web pages less complicated than using traditional ASP, but in the end, the page sends standard HTML to the client’s browser. HTML Controls
|