Elements of an ASP.NET Page

IOTA^_^    

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


Let's examine a typical ASP.NET page, shown in Listing 1.5. This page displays a message to the user and a form that lets her enter her name. When she clicks the Submit button, she sees a customized welcome message.

Listing 1.5 Interacting with the User
 1:    <%@ Page Language="VB" %> 2: 3:    <script runat="server"> 4:       Sub tbMessage_Change(Sender As Object, E As EventArgs) 5:          lblMessage.Text = "Hello " + tbMessage.Text 6:       End Sub 7:    </script> 8: 9:    <html><body> 10:       <font size="5">Sam's Teach Yourself ASP.NET in 21 Days: 11:       Day 2</font><hr><p> 12:       <% Response.Write("Our First Page<p>") %> 13: 14:       <form runat="server"> 15:          Please enter your name: 16:          <asp:textbox  17:             OnTextChanged="tbMessage_Change" 18:             runat=server/> 19:          <asp:button  Text="Submit" 20:             runat=server/><p> 21:          <asp:label  font-size="20pt" 22:             runat=server/> 23:       </form> 24:    </body></html> 

Save this page as listing0105.aspx in the c:\inetpub\wwwroot\tyaspnet21days\day1 directory. View it from a Web browser with the URL http://localhost/tyaspnet21days/day1/listing0105.aspx. Enter your name in the box and click Submit. You'll see a nice hello message, as shown in Figure 1.12.

Figure 1.12. A simple ASP.NET page that displays a personalized welcome message.

graphics/01fig12.gif

graphics/analysis_icon.gif

This page highlights the most common elements found in ASP.NET pages. On line 1 is the <%@ Page %> directive, which supplies the ASP.NET page with specific information that's used during the compiling process. In this case, you're telling ASP.NET that you're using the Visual Basic.NET programming language to write your code (you can use "C#" or "CS" to signify that you want to use C# for the programming language).

graphics/newterm_icon.gif

Lines 3 7 contain a block of code called a code declaration block. This looks similar to the client-side code that you learned about earlier today in "Client-Side Processing," but it includes a new tag, runat="server". This is the code that ASP.NET uses to process its pages, and it's where you'll control all the functionality. This code is also compiled into MSIL.

Tip

If you're familiar with client-side programming, you know that typically a <script> block is placed in the <head>...</head> tags in HTML. These blocks, however, can be placed anywhere in the page, and placing them at the very top is a good way to keep your ASP.NET code separate from the HTML code.


graphics/newterm_icon.gif

Starting on line 9, you begin the HTML page. This is the content that will be sent to the browser (along with whatever your ASP.NET code spits out). Line 12 begins with <%. This is known as a code render block. It contains additional instructions that ASP.NET uses to produce output. In this case, it tells ASP.NET to write "Our First Page" to the browser. Code render blocks aren't compiled, and as such they're not as efficient as code declaration blocks. You'll be using them sparingly throughout this book.

graphics/newterm_icon.gif

On line 14 you have another traditional HTML element, but then there's that runat="server" again. When you specify this tag, the form becomes a Web form. Anything contained within a Web form can be watched over by ASP.NET. (Recall the analogy of the librarian and his spies.)

graphics/newterm_icon.gif

On lines 16, 19, and 21, you have a few new elements that look like HTML elements. These are known as Web controls. Often they perform very similarly to HTML elements, but with added functionality that ASP.NET can use. Notice that each of these items also has the runat="server" line.

That's your first look at an ASP.NET page. As you can see, the code is easy to read.


    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