03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

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

Us<strong>in</strong>g <strong>ASP</strong>.<strong>NET</strong> AJAX <strong>in</strong> Your Projects x 351<br />

Even though this model has been used for years to serve web pages, it has a few big drawbacks. First,<br />

because the entire page is loaded after a postback, the HTML sent to the browser is much larger than<br />

it needs to be. Th<strong>in</strong>k back to the contact form you created <strong>in</strong> the previous chapter. Right after the user<br />

has submitted the contact form, the server shows a Label control with the text Message Sent. It does<br />

that by fully load<strong>in</strong>g a new page that hides the form controls <strong>and</strong> shows the message. Even though the<br />

rest of the page hasn’t changed (the menu, the sidebar, the footer, <strong>and</strong> so on), they are still sent from<br />

the server to the client. Ideally, you would only want to send back the HTML that has changed. In the<br />

case of the contact form, that could be as little as the text Message Sent. The right-h<strong>and</strong> side of<br />

Figure 10-1 shows how this works. Rather than send<strong>in</strong>g the entire page as a response, the server<br />

sends a partial response (conta<strong>in</strong><strong>in</strong>g little more than the text Message Sent), which is then used by the<br />

browser to update just the part of the page that has changed, leav<strong>in</strong>g the rest of the page as it was.<br />

The second drawback of a full page reload has to do with the way the browser renders the page.<br />

Because the entire page is replaced, the browser has to dismiss the old one <strong>and</strong> then draw the new<br />

one. This causes the page to “flicker,” which results <strong>in</strong> an unattractive user experience. You can<br />

deploy Ajax techniques to overcome these two problems, as you see <strong>in</strong> the rema<strong>in</strong>der of this chapter.<br />

The concepts beh<strong>in</strong>d Ajax have been around for many years. Browsers s<strong>in</strong>ce Internet Explorer 5<br />

have shipped with the XMLHttpRequest object that enabled you to make calls to the server from<br />

JavaScript to send <strong>and</strong> receive data. However, people also used other techniques to emulate the<br />

behavior of what is now called Ajax, <strong>in</strong>clud<strong>in</strong>g Macromedia Flash, iframe elements, or hidden<br />

frames.<br />

However, when the term Ajax was <strong>in</strong>troduced, th<strong>in</strong>gs really took off. In an attempt to stay ahead of<br />

the curve, Microsoft started build<strong>in</strong>g <strong>ASP</strong>.<strong>NET</strong> AJAX, the Ajax framework that is now fully <strong>in</strong>tegrated<br />

<strong>in</strong> <strong>ASP</strong>.<strong>NET</strong> <strong>and</strong> Visual Studio 2012. This framework offers a number of benefits that you as<br />

a web developer can take advantage of to create responsive applications.<br />

In particular, <strong>ASP</strong>.<strong>NET</strong> AJAX enables you to:<br />

‰ Create flicker-free pages that enable you to refresh portions of the page without a full reload<br />

<strong>and</strong> without affect<strong>in</strong>g other parts of the page<br />

‰ Provide feedback to your users dur<strong>in</strong>g these page refreshes<br />

‰ Update sections of a page <strong>and</strong> call server-side code on a scheduled basis us<strong>in</strong>g a timer<br />

‰ Access server-side WCF services <strong>and</strong> page methods <strong>and</strong> work with the data they return<br />

The nice th<strong>in</strong>g about <strong>ASP</strong>.<strong>NET</strong> AJAX is that it is very easy to get started with. Creat<strong>in</strong>g a flickerfree<br />

page is a matter of dragg<strong>in</strong>g <strong>and</strong> dropp<strong>in</strong>g a few controls from the Toolbox onto your page.<br />

When you underst<strong>and</strong> the basics of the Ajax framework, you can extend your knowledge by look<strong>in</strong>g<br />

at more advanced topics such as call<strong>in</strong>g WCF services.<br />

USING <strong>ASP</strong>.<strong>NET</strong> AJAX IN YOUR PROJECTS<br />

<strong>ASP</strong>.<strong>NET</strong> AJAX is fully <strong>in</strong>tegrated <strong>in</strong> <strong>ASP</strong>.<strong>NET</strong> <strong>and</strong> VS, which means you can start us<strong>in</strong>g it right<br />

away. Each new <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> web project you create <strong>in</strong> VS is already Ajax-enabled. In addition, the<br />

Toolbox conta<strong>in</strong>s an AJAX Extensions category with a number of Ajax-related controls that you can

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

Saved successfully!

Ooh no, something went wrong!