15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

asP.neT Web forms ❘ 1155<br />

➤<br />

➤<br />

The complete code is compiled <strong>and</strong> cached ready to process subsequent requests.<br />

HTML is generated <strong>and</strong> returned to the user.<br />

In web applications the process is similar, although no dynamic compilation is required as web applications<br />

are precompiled by definition.<br />

The code-behind file generated for you in the PCSWebSite1 web site for Default.aspx is initially very<br />

sparse. First, you see the default set of namespace references that you are likely to use in ASP.<strong>NET</strong><br />

web pages:<br />

using System;<br />

using System.Collections.Generic;<br />

using System.Linq;<br />

using System.Web;<br />

using System.Web.UI;<br />

using System.Web.UI.WebControls;<br />

code snippet PCSWebSite1/Default.aspx.cs<br />

Below these references, you see an almost completely empty partial class definition for Default_aspx:<br />

public partial class _Default : System.Web.UI.Page<br />

{<br />

protected void Page_Load(object sender, EventArgs e)<br />

{<br />

}<br />

}<br />

Here, the Page_Load() event h<strong>and</strong>ler can be used to add any code that is required when the page is loaded.<br />

As you add more event h<strong>and</strong>lers, <strong>and</strong> so on, this class file will become increasingly full of code. Note that<br />

you don’t see the code that wires up this event h<strong>and</strong>ler to the page — event h<strong>and</strong>lers are inferred by the ASP.<br />

<strong>NET</strong> runtime, as noted earlier. This is due to the AutoEventWireUp attribute — setting this to false will<br />

mean that you will need to associate the event h<strong>and</strong>lers in your code with events on your own.<br />

This class is a partial class definition because the process outlined earlier requires it. When the page<br />

is precompiled, a separate partial class definition is created from the ASP.<strong>NET</strong> code for your page.<br />

This includes all the controls you have added to the page. At design time, the compiler infers this partial<br />

class definition, which allows you to use IntelliSense in your code-behind to reference controls on your page.<br />

asP.neT server Controls<br />

Your generated code doesn’t do very much yet, so next you need to add some content. You can do this in<br />

Visual Studio using the Web Form designer, which supports drag-<strong>and</strong>-drop in just the same way as the<br />

Windows Forms designer.<br />

You can add three types of server controls to your ASP.<strong>NET</strong> pages:<br />

➤<br />

➤<br />

➤<br />

HTML server controls — These controls mimic HTML elements, which will be familiar to HTML<br />

developers.<br />

Web server controls — This is a new set of controls, some of which have the same functionality as<br />

HTML controls. These controls have a common naming scheme for properties <strong>and</strong> other elements to<br />

ease development, <strong>and</strong> provide consistency with analogous Windows Forms controls. There are also<br />

some completely new <strong>and</strong> very powerful controls, as you will see later. Several types of web server<br />

controls exist, including the st<strong>and</strong>ard ones such as buttons, validation controls for validating user<br />

input, login controls to simplify user management, <strong>and</strong> more complicated controls for dealing with<br />

data sources.<br />

Custom <strong>and</strong> user controls — These controls are defined by the developer <strong>and</strong> can be created in a<br />

number of ways, as discussed in Chapter 41.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!