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.

260<br />

CHAPTER 11 ■ <strong>AJAX</strong> APPLICATIONS AND EMPOWERING THE WEB USER EXPERIENCE<br />

The first thing you’ll do is split the application into two pages: the page that runs on<br />

the server <strong>and</strong> provides the calculation, <strong>and</strong> the page that provides the UI <strong>and</strong> calls the<br />

first page.<br />

To add the server page, add a new <strong>ASP</strong>.<strong>NET</strong> <strong>Web</strong> Forms page to your project, <strong>and</strong> call<br />

it Multiply<strong>AJAX</strong>Server.aspx.<br />

In Source view, remove all the markup on the page except for the <strong>ASP</strong>.<strong>NET</strong> markup on<br />

the top line, which looks like this:<br />

<br />

Now, in the Page_Load event h<strong>and</strong>ler, add this code:<br />

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

{<br />

int nX = 0;<br />

int nY = 0;<br />

try<br />

{<br />

nX = Convert.ToInt16(Request.Params["nx"]);<br />

nY = Convert.ToInt16(Request.Params["ny"]);<br />

}<br />

catch (Exception ex)<br />

{<br />

nX = 0;<br />

nY = 0;<br />

}<br />

int nAns = nX * nY;<br />

Response.Write(nAns);<br />

}<br />

This takes two HTTP parameters, nX <strong>and</strong> nY, <strong>and</strong> multiplies them out, writing the<br />

response back to the output buffer. You’ll take these results <strong>and</strong> load them into the<br />

answer field when you write the Ajax UI layer. This is why you removed all the markup<br />

from the page earlier, as you do not want to write out unnecessary HTML tags from this<br />

service (such as <strong>and</strong> ).<br />

To create the Ajax UI, add a new HTML page to the solution <strong>and</strong> call it<br />

Multiply<strong>AJAX</strong>Client.htm. Make a similar layout to the earlier example, but use HTML<br />

controls. When you are ready, the HTML should look like this:

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

Saved successfully!

Ooh no, something went wrong!