15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1154 ❘ ChaPTer 40 cOre Asp.net<br />

Underneath the (currently empty) form, you can see where in the HTML for the form the cursor is currently<br />

positioned. Here the cursor is in a element inside a element inside the element of the<br />

page. The element is displayed as to identify the element by its id attribute, which<br />

you will see shortly. The element is also labeled in the design view.<br />

The source view for the page shows you the code generated inside the .aspx file:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet PCSWebSite1/Default.aspx<br />

If you know any HTML syntax, then this will look familiar to you. You are presented with the basic<br />

code required for an HTML page following the XHTML schema, with a few extra bits of code. The most<br />

important extra is the element, which has an id attribute of form1. This element will contain your<br />

ASP.<strong>NET</strong> code. The most important thing to note here is the runat attribute. Just as with the server-side<br />

code blocks you saw at the start of this section, this is set to server, meaning that the processing of the<br />

form will take place on the server. If you don’t include this reference, then no server-side processing will<br />

be performed, <strong>and</strong> the form won’t do anything. There can be only one server-side element in an<br />

ASP.<strong>NET</strong> page.<br />

The other interesting thing about this code is the tag at the top. This tag defines page<br />

characteristics that are important to you as a <strong>C#</strong> web developer. There is a Language attribute that specifies<br />

that you will use <strong>C#</strong> throughout your page, as you saw earlier with blocks. (The default for ASP.<br />

<strong>NET</strong> pages is Visual Basic .<strong>NET</strong>, although this can be changed using a Web.config file, which you will see<br />

later in this chapter.) The other three attributes — AutoEventWireup, CodeFile, <strong>and</strong> Inherits — are used<br />

to associate the Web Form with a class in a code-behind code file, in this case the partial class _Default in<br />

the file Default.aspx.cs. This leads straight into a necessary discussion about the ASP.<strong>NET</strong> code model.<br />

The asP.neT Code model<br />

In ASP.<strong>NET</strong>, a combination of layout (HTML) code, ASP.<strong>NET</strong> controls, <strong>and</strong> <strong>C#</strong> code is used to generate the<br />

HTML that users see. The layout <strong>and</strong> ASP.<strong>NET</strong> code are stored in an .aspx file, such as the one you looked<br />

at in the preceding section. The <strong>C#</strong> code that you add to customize the behavior of the form is contained<br />

either in the .aspx file or, as in the preceding example, in a separate .aspx.cs file, which is usually referred<br />

to as the “code-behind” file.<br />

In a web site project, when an ASP.<strong>NET</strong> Web Form is processed — typically when a user requests the page,<br />

although sites can be precompiled — several things happen:<br />

➤<br />

➤<br />

➤<br />

The ASP.<strong>NET</strong> process examines the page <strong>and</strong> determines what objects must be created to instantiate<br />

the page object model.<br />

A base class for the page is dynamically created, including members for the controls on the page as<br />

well as event h<strong>and</strong>lers for these controls (such as button click events).<br />

Additional code contained in the .aspx page is combined with this base class to complete the<br />

object model.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!