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.

asP.neT Web forms ❘ 1151<br />

Conversely, web applications are compiled before being deployed to a web server, <strong>and</strong> don ’ t include .cs<br />

fi le s . I n s t e ad , a pre - c ompi le d a s s e mbly i s deploye d to t he s er ver a long w it h A S P. N E T pa ge s .<br />

In general, most ASP.<strong>NET</strong> programmers tend to prefer the web application model, <strong>and</strong> there are some<br />

technologies (such as MVC) that will only work with web applications. Web sites are mainly useful in<br />

the development stage, where quick changes to code may be required <strong>and</strong> a full deployment may not be<br />

desirable.<br />

state management in asP.neT<br />

One of the key properties of ASP.<strong>NET</strong> pages is that they are effectively stateless. By default, no information<br />

is stored on the server between user requests (although there are methods for doing this, as you will see later<br />

in this chapter). At fi rst glance, this seems a little strange because state management is something that seems<br />

essential for user - friendly interactive sessions. However, ASP.<strong>NET</strong> provides a workaround to this problem,<br />

such that session management becomes almost transparent.<br />

In short, information such as the state of controls on a Web Form (including data entered in text boxes<br />

or selections from drop - down lists) is stored in a hidden viewstate fi eld that is part of the page generated<br />

by the server <strong>and</strong> passed to the user. Subsequent actions, such as triggering events that require server - side<br />

processing, such as submitting form data, result in this information being sent back to the server; this is<br />

known as a postback operation. On the server, this information is used to repopulate the page object model<br />

allowing you to operate on it as if the changes had been made locally.<br />

You will see this in action shortly <strong>and</strong> examine the details.<br />

asP.neT Web forms<br />

As mentioned earlier, much of the functionality in ASP.<strong>NET</strong> is achieved using Web Forms. Before long,<br />

you will dive in <strong>and</strong> create a simple Web Form to give you a starting point to explore this technology. First,<br />

however, this section reviews some key points pertinent to Web Form design.<br />

Note that some ASP.<strong>NET</strong> developers simply use a text editor such as Notepad to create fi les. We wouldn ’ t<br />

advocate this ourselves because the benefi ts you get via an IDE, such as Visual Studio or Web Developer<br />

Express are substantial, but it ’ s worth mentioning because it is a possibility. If you do take this route, you<br />

have a great deal of fl exibility as to which parts of a web application you put where. This enables you, for<br />

example, to combine all your code in one fi le. You can achieve this by enclosing code in elements,<br />

using two attributes on the opening tag:<br />

< script language="c#" runat="server" ><br />

// Server-side code goes here.<br />

< /script ><br />

The runat= “ server ” attribute here is crucial because it instructs the ASP.<strong>NET</strong> engine to execute this code<br />

on the server rather than sending it to the client, thus giving you access to the rich environment hinted at<br />

earlier. You can place your functions, event h<strong>and</strong>lers, <strong>and</strong> so on in server - side script blocks.<br />

If you omit the runat= “ server ” attribute, you are effectively providing client - side code, which will fail if<br />

it uses any of the server - side style coding that is discussed in this chapter. You can, however, use <br />

elements to supply client - side script in languages such as JavaScript. For example:<br />

< script language="JavaScript" type="text/JavaScript" ><br />

// Client-side code goes here; you can also use "vbscript".<br />

< /sc r i p t ><br />

Note that the type attribute here is optional, but necessary if you want to comply with<br />

HTML <strong>and</strong> XHTML st<strong>and</strong>ards.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!