08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

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

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

58<br />

CHAPTER 3 ■ WEB FORMS WITH <strong>ASP</strong>.<strong>NET</strong><br />

h<strong>and</strong>le the HTTP-POST from the form. Other initialization, such as configuring controls,<br />

retrieving database information, <strong>and</strong> so on, is usually done at this stage so that when the<br />

page is rendered, it is provided with all the information the user needs.<br />

It’s important to note that Page_Load always fires, whether this is the first time the<br />

page has loaded or it is a postback. With the latter, you might expect it not to load,<br />

because the page is already loaded, but it does. Due to the stateless nature of web applications,<br />

each postback is treated as a new request to the page, <strong>and</strong> view state is used to<br />

maintain state between calls, as discussed earlier.<br />

As a developer, you may need to discern whether you are in a postback situation or<br />

an initial load situation, <strong>and</strong> <strong>ASP</strong>.<strong>NET</strong> provides this information for you via the IsPostBack<br />

property on the Page object.<br />

Thus, Page.IsPostBack will be true upon a postback <strong>and</strong> false on the first load. If<br />

there are actions that you only want to perform on either case, you can gate them inside<br />

an if(Page.IsPostBack) clause.<br />

Performing Validation<br />

An important aspect of any web application is verifying that the data the user has input is<br />

valid. This ensures that your application is secure (e.g., so that no one is able to attempt<br />

Structured Query Language [SQL] injection attacks) <strong>and</strong> the user experience is optimized;<br />

only appropriate data is sent to the server <strong>and</strong> returned from the server. <strong>ASP</strong>.<strong>NET</strong><br />

introduces a suite of self-sufficient validation controls that can be used to ensure that the<br />

content of your page meets validation rules. These are very useful controls, relieving you<br />

from writing reams of validation code to ensure that the correct data has been input.<br />

Performing Event H<strong>and</strong>ling<br />

Earlier we saw how events are h<strong>and</strong>led on a page. Some events provide an automatic<br />

postback that triggers an event h<strong>and</strong>ler. Others—such as a list box selection—aren’t h<strong>and</strong>led<br />

immediately, unless they have their AutoPostBack property set to true. At this stage<br />

of the life cycle, <strong>ASP</strong>.<strong>NET</strong> invokes the event h<strong>and</strong>lers for each of the events that were<br />

tripped. So, if your page has a number of events before the postback occurs—for example,<br />

if a number of lists are used with no AutoPostBack, <strong>and</strong> the user selects items on them<br />

before pressing a button on the page—then each of these events will have their h<strong>and</strong>ler<br />

called at this stage.<br />

In addition, a number of page-level events need to be performed. In the scenario in<br />

which two lists do not have AutoPostBack set to true, <strong>and</strong> there’s no button that causes the<br />

postback, events will be fired in the following order:<br />

1. Page_Init<br />

2. Page_Load

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

Saved successfully!

Ooh no, something went wrong!