15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

What is ajax ❘ 1239<br />

The code that achieves this is:<br />

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(<br />

EndRequestH<strong>and</strong>ler);<br />

function EndRequestH<strong>and</strong>ler(sender, args)<br />

{<br />

if (args.get_error() != undefined)<br />

{<br />

var errorMessage = args.get_error().message;<br />

args.set_errorH<strong>and</strong>led(true);<br />

$get('errorDisplay').innerHTML = errorMessage;<br />

}<br />

}<br />

Note that the errorH<strong>and</strong>led property of the EndRequestEventArgs object is set to true. This prevents the<br />

default behavior, which is to display the error message in a dialog box, by using the JavaScript alert() function.<br />

The error itself is generated by throwing an exception on the server as follows:<br />

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

{<br />

throw new ApplicationException(<br />

"This is the message set in the exception on the server.");<br />

}<br />

There are many other situations when you will want to use event h<strong>and</strong>ling techniques to act on the<br />

Application <strong>and</strong> PageRequestManager events.<br />

Javascript Debugging<br />

In the past, JavaScript has had a reputation of being difficult to debug. However, this has been addressed in<br />

the latest version of Visual Studio. You can now add breakpoints <strong>and</strong> step through JavaScript code just like <strong>C#</strong><br />

code. You can also interrogate object state in break mode, change property values, <strong>and</strong> so on. The IntelliSense<br />

that is available when you write JavaScript code is also vastly improved in the latest version of Visual Studio.<br />

However, there will still be times when you will want to add debug <strong>and</strong> trace code to report information as<br />

code is executed. For example, you might want to use the JavaScript alert() function to show information<br />

in dialog boxes.<br />

There are also some third-party tools that you can use to add a client-side UI for debugging. These include:<br />

➤<br />

➤<br />

Fiddler — This tool, which you can obtain from www.fiddlertool.com, enables you to log all HTTP<br />

traffic between your computer <strong>and</strong> a web application — including partial-page postbacks. There are<br />

also additional tools that you can use to look at what occurs during the processing of web pages in<br />

more detail.<br />

Nikhil’s Web Development Helper — This tool, available at http://projects.nikhilk.net/<br />

WebDevHelper, can also log HTTP traffic. In addition, this tool contains a number of utilities specifically<br />

aimed at ASP.<strong>NET</strong> <strong>and</strong> ASP.<strong>NET</strong> AJAX development, for example, the ability to examine view<br />

state <strong>and</strong> to execute immediate JavaScript code. This latter feature is particularly useful to test objects<br />

that you may have created on the client. The Web Development Helper also displays extended error<br />

information when JavaScript errors occur, which makes it easier to track down bugs in JavaScript code.<br />

The AJAX Library also provides the Sys.Debug class, which you can use to add some extra debugging features<br />

to your application. One of the most useful features of this class is the Sys.Debug.traceDump() function, which<br />

enables you to analyze objects. One way to use this function is to place a textarea control on your web page<br />

with an id attribute of TraceConsole. Then, all output from Debug will be sent to this control. For example,<br />

you can use the traceDump() method to output information about the Application object to the console:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!