Creating ASP.NET Pages

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Day 1.  Getting Started with ASP.NET


ASP.NET pages are simply pure text, like HTML files. Once you have a Web server and the .NET Framework SDK up and running, you can easily create ASP.NET pages in any text editor you choose.

ASP.NET pages have the .aspx extension, so any files you want the server to interpret as ASP.NET pages must end in .aspx, such as default.aspx. Let's create a simple file to get started. Open Notepad (or your editor of choice) and type in the code in Listing 1.2. (Don't worry about understanding it yet. We'll get there soon enough.)

Listing 1.2 Your First ASP.NET Page in VB.NET
 1:    <%@ Page Language="VB" %> 2: 3:    <script runat="server"> 4:       sub Page_Load(obj as object, e as eventargs) 5:          lblMessage.Text = "Welcome to ASP.NET!" 6:       end sub 7:    </script> 8: 9:    <html><body> 10:       <asp:Label  runat="server"/> 11:    </body></html> 

Listing 1.2 was written using VB.NET, but recall that you can use multiple programming languages to write ASP.NET pages. Listing 1.3 shows the exact same page using C# we'll examine the differences in Day 4, "Using ASP.NET Objects with C# and VB.NET."

Listing 1.3 Your First ASP.NET Page in C#
 1:  <%@ Page Language="C#" %> 2: 3:  <script runat="server"> 4:     void Page_Load(Object obj, EventArgs e) { 5:        lblMessage.Text = "Welcome to ASP.NET!"; 6:     } 7:  </script> 8: 9:  <html><body> 10:     <asp:Label  runat="server"/> 11:  </body></html> 

Create a new directory named day1 in your c:\inetpub\wwwroot folder and save this file as listing0102.aspx (or listing0103.aspx for the C# version). This page simply displays a welcome message to visitors. Open your Web browser and access this page with the URL http://localhost/day1/listing0102.aspx. You should see the window shown in Figure 1.11.

Figure 1.11. Your first ASP.NET welcome page!

graphics/01fig11.gif

Tip

Throughout this book, you'll be creating a new directory for each lesson. Each directory will be placed in its own c:\inetpub\wwwroot\tyaspnet21days folder. For example, Day 2's ASP.NET pages will go into the c:\inetpub\wwwroot\tyaspnet21days\day2 folder. This directory will then be accessible through the browser via http://localhost/tyaspnet21days/day2. This will make things easy to find.


Remember that browsers can only understand HTML. Right-click on this output and select View source. You should see the code in Listing 1.4.

Example 1.4 The HTML from Your ASP.NET Page
 1:    <html><body> 2:       <span >Welcome to ASP.NET!</span> 3:    </body></html> 

What happened to the rest of the code in listing0102.aspx (see Listing 1.2)? ASP.NET compiled it into MSIL, which was then compiled into machine language by the CLR and executed. The output from the execution is what you see in Listing 1.4. ASP.NET translated all of its output to HTML because it's the only language that browsers can read.

Development Environments

As much as you may like Notepad, it's not the ideal application for creating ASP.NET pages. It's easy and fast to use, but doesn't offer many features that would make ASP.NET development easier.

Microsoft Visual Studio.NET (VS.NET) is another commonly used editor. This powerful product allows you to manage entire Web sites, and it provides features such as creating and deleting virtual directories, working with databases, and dragging and dropping HTML components. It even color-codes your ASP.NET code to make it easier to read.

Note

Older versions of Visual Studio (6.0 and below) will work as well. However, they don't support the .NET Framework, so some features won't work correctly. For example, older versions won't color-code your ASP.NET code.


Another common environment is Microsoft FrontPage. This is a visual tool that allows you to create Web pages without having to write any HTML code. Unfortunately, it won't write ASP.NET code for you, so you'll have to do that yourself. For more information, check out Sams Teach Yourself FrontPage in 21 Days.

None of the pages you'll create in this book require more than Notepad, so the development environment is entirely up to you. The additional features provided by Visual Studio.NET and FrontPage may be of use, but these programs lack the sheer simplicity of Notepad. There are also quite a few non-Microsoft editors out there, such as HoTMetaL. Many of them don't yet support the .NET Framework, however, so they may not be as functional as the Microsoft editors.

Essentially, use what you are most comfortable with. This book will stick to Notepad because it is easy to use and is universally available. Visual Studio.NET and FrontPage are both commercial products that may be out of many people's budgets.


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

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