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

Create successful ePaper yourself

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

310<br />

CHAPTER 13 ■ <strong>AJAX</strong> SCRIPTS AND SERVICES<br />

not the whole page). Should you set it to false, then full page refreshes will occur on<br />

postbacks, unless you write JavaScript code that uses the XMLHttpRequest class to perform<br />

asynchronous postbacks <strong>and</strong> then manually update the page. Typically for Ajax pages,<br />

you will always set this to true. If your page is going to be relatively static (i.e., no postbacks<br />

to the server), <strong>and</strong> you want to use a ScriptManager to deliver JavaScript, then you<br />

can set it to false to reduce overhead.<br />

H<strong>and</strong>ling Errors in Partial Page Updates<br />

In case you hit an error in the process of a partial page update, the ScriptManager has a<br />

property called AllowCustomErrorsRedirect (defaulting to true), which determines that<br />

the custom errors section of the <strong>Web</strong>.config file should be used when an error occurs—<br />

namely that you can redirect the user to a friendly l<strong>and</strong>ing page instead of crashing them<br />

out of your app.<br />

Alternatively, you can catch <strong>and</strong> h<strong>and</strong>le the AsyncPostBackError event, <strong>and</strong>/or set<br />

the AsyncPostBackErrorMessage property to manage errors. To do this, you define the<br />

AsyncPostBackError callback by setting the OnAsyncPostBackError attribute on the Script-<br />

Manager control. Here’s an example:<br />

<br />

<br />

Then, in your server code, you can implement a function that h<strong>and</strong>les this error. In<br />

this case, it takes the exception message <strong>and</strong> loads it into the AsyncPostBackErrorMessage<br />

property, which is accessible from client-side script via the PageRequestManager (you’ll<br />

see more of this in the next chapter).<br />

protected void ScriptManager1_AsyncPostBackError(object sender,<br />

AsyncPostBackErrorEventArgs e)<br />

{<br />

ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message;<br />

}<br />

For example, if you add an EndRequestH<strong>and</strong>ler callback to the PageRequestManager,<br />

like this:<br />

Sys.<strong>Web</strong>Forms.PageRequestManager.getInstance().add_endRequest(EndRequestH<strong>and</strong>ler);<br />

you can then access the AsyncPostBackErrorMessage from your JavaScript code as part of<br />

the args sent back to the EndRequestH<strong>and</strong>ler:

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

Saved successfully!

Ooh no, something went wrong!