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.

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

workflow that HTML forms offer, something is necessary to bubble the events up to the<br />

server so that the server knows what event to respond to. The flow of processing follows<br />

this model:<br />

• When the page first runs, <strong>ASP</strong>.<strong>NET</strong> parses through the declarations <strong>and</strong> generates<br />

HTML to be sent to the client. All objects that it uses are destroyed from server<br />

memory once the HTML has been generated <strong>and</strong> issued to the client.<br />

• The user triggers a postback to the server, either by clicking an HTML submit button<br />

or performing an action that the server will interpret as an event, such as<br />

checking a CheckBox control.<br />

• When the page is returned to the server, <strong>ASP</strong>.<strong>NET</strong> is able to interpret the view state<br />

information to restore the state of each of the controls.<br />

• The view state information also contains the operation that triggered the postback.<br />

<strong>ASP</strong>.<strong>NET</strong> obtains this information <strong>and</strong> raises the appropriate event, <strong>and</strong> if you’ve<br />

written an event h<strong>and</strong>ler for this event, then your event h<strong>and</strong>ler’s code will execute.<br />

Other events that didn’t cause the postback will also fire, such as a list selection,<br />

<strong>and</strong> if you’ve provided an event h<strong>and</strong>ler for those events, you can process them<br />

as well.<br />

• The modified page is rendered back into HTML <strong>and</strong> returned to the client, <strong>and</strong> all<br />

objects—including those invoked by the event h<strong>and</strong>lers—are destroyed.<br />

Not all client browser events fire a postback right away, <strong>and</strong> <strong>ASP</strong>.<strong>NET</strong> is designed to<br />

process each event at the most appropriate time. For example, if you come from a Windows<br />

development background, you may know that the Windows Forms TextBox fires a<br />

TextChanged event whenever the user types something into the text box. However, in a<br />

web context, you wouldn’t want to do this, because you’d trigger a postback every time<br />

you press a key when the cursor is in a text box, severely hampering your application’s<br />

performance <strong>and</strong> scalability. Instead, the web-based text box triggers a postback when<br />

you press Enter or Tab if so configured.<br />

However, when you start getting into complex controls, you may not want to use a<br />

postback for every user interaction. Consider a scenario where you have a UI containing<br />

a list control. In some cases, you might want to have a postback when the user selects an<br />

item on the list (so that you can respond to it right away), <strong>and</strong> in other cases, you might<br />

not want one. You can set up the former situation using an automatic postback (using the<br />

control’s AutoPostback property), where the control triggers the postback instead of the<br />

user. For the latter, just query the status of the list when the user selects a submit button<br />

or something similar to generate a postback for the page.<br />

Listing 3-10 shows a simple web UI containing a three-item list. This list has an event<br />

h<strong>and</strong>ler defined for it that is designated to fire whenever the list selection changes.

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

Saved successfully!

Ooh no, something went wrong!