10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

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

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

Chapter 9<br />

this.txtTitle.Attributes.Add(“onmouseout”,<br />

“ChangeCSSClass(this,’mouseOut’);”);<br />

}<br />

if (this.Session[“title”] != null)<br />

{<br />

this.txtTitle.Text = this.Session[“title”].ToString();<br />

}<br />

}<br />

}<br />

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

{<br />

this.Session.Add(“title”,this.txtTitle.Text);<br />

this.txtTitle.AutoUpdateAfterCallBack = true;<br />

}<br />

Although much of this implementation is similar to the Magic<strong>Ajax</strong> demonstration, notice the different<br />

approaches. Using Anthem.<strong>NET</strong> you declare an Anthem TextBox control and use the built-in mechanism<br />

of firing the TextChanged event to initiate a request to the server. Once the server is contacted, you<br />

can apply the Session value and set the AutoUpdateAfterCallBack property on the TextBox control<br />

equal to true.<br />

You can now test the behavior by launching the page in the browser.<br />

Example 7: Server Exceptions<br />

A feature exclusive to Anthem.<strong>NET</strong> from all the frameworks listed in this section is the ability to deal<br />

with unhandled server-side exceptions. In other frameworks, should your application encounter an<br />

unhandled exception, the error may bubble up to the user interface as the cryptic and intimidating stock<br />

yellow and white <strong>ASP</strong>.<strong>NET</strong> error page. While the subject of structured exception management is out of<br />

the scope of this book, you will implement an example that will enforce this foundational rule: protect<br />

the user from useless error messages.<br />

Anthem.<strong>NET</strong> will wrap up unhandled exceptions into a JSON error object. When you encounter this<br />

object, you may then decide what to do on the client with the error. The response object returned by<br />

Anthem.<strong>NET</strong> includes an error object in its interface. If the error property is not null, then you have<br />

encountered an error on the server.<br />

Figure 9-17 shows an example of how the page will appear when Anthem wraps up an unhandled<br />

server exception.<br />

244

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

Saved successfully!

Ooh no, something went wrong!