15.02.2015 Views

C# 4 and .NET 4

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

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

1234 ❘ ChaPTer 41 Asp.net feAtures<br />

using the ajaX library<br />

There is a great deal of functionality available in the AJAX Library that you can use to further enhance your<br />

web applications. However, to do this you need at least a working knowledge of JavaScript. In this section,<br />

you see some of this functionality, although this is not an exhaustive tutorial.<br />

The basic principles behind the use of the AJAX Library are much the same as for adding any type of clientside<br />

script to a web application. You will still use the core JavaScript language, <strong>and</strong> you will still interact<br />

with the DOM. However, there are many areas where the AJAX Library makes things easier for you.<br />

This section explains many of these areas <strong>and</strong> provides a foundation that you can build on with further<br />

experimentation <strong>and</strong> study of the online AJAX Library documentation.<br />

The techniques covered in this section are illustrated in the PCSLibraryDemo project, which is referred to<br />

throughout the rest of this chapter.<br />

adding Javascript to a Web Page<br />

The first thing you need to know is how to add client-side JavaScript to a web page. You have three<br />

options here:<br />

➤<br />

➤<br />

➤<br />

Add JavaScript inline in ASP.<strong>NET</strong> web pages by using the element.<br />

Add JavaScript to separate JavaScript files with the extension .js <strong>and</strong> reference these files from <br />

elements or (preferably) by using the child element of the ScriptManager control.<br />

Generate JavaScript from server-side code, such as code behind or custom extender controls.<br />

Each of these techniques has its own benefits. For prototyping code, there is no substitute for inline code<br />

because it is so quick <strong>and</strong> easy to use. You will also find it easy to associate client-side event h<strong>and</strong>lers of<br />

HTML elements <strong>and</strong> server controls with client-side functions, because everything is in the same file.<br />

Having separate files is good for reusability, because you may create your own library of classes much like<br />

the existing AJAX Library JavaScript files.<br />

Generating code from code-behind can be tricky to implement because you will not usually have access to<br />

IntelliSense for JavaScript programming when you use <strong>C#</strong> code. However, you will be able to generate code<br />

dynamically in response to application state, <strong>and</strong> sometimes this is the only way to do things.<br />

The extenders that you can create with the AJAX Control Toolkit include a separate JavaScript file that<br />

you use to define behaviors, which gets around some of the problems of exposing client-side code from<br />

the server.<br />

In this chapter, you use the inline code technique, because it is simplest <strong>and</strong> allows you to concentrate on the<br />

JavaScript functionality.<br />

Global Utility functions<br />

One of the features supplied by the AJAX Library that you will use most often is the set of global functions<br />

that wrap other functionality. These include the following:<br />

➤<br />

➤<br />

➤<br />

$get() — This function enables you to get a reference to a DOM element by supplying its client-side<br />

id value as a parameter, with an optional second parameter to specify the parent element to search in.<br />

$create() — This function enables you to create objects <strong>and</strong> perform initialization at the same time.<br />

You can supply between one <strong>and</strong> five parameters to this function. The first parameter is the type you<br />

want to instantiate, which will typically be a type defined by the AJAX Library. The other parameters<br />

enable you to specify initial property values, event h<strong>and</strong>lers, references to other components, <strong>and</strong> the<br />

DOM element that the object is attached to, respectively.<br />

$addH<strong>and</strong>ler() — This function provides a shorth<strong>and</strong> for adding an event h<strong>and</strong>ler to an object.<br />

There are more global functions, but these are the ones you will use most often. $create() in particular is<br />

a very useful way to reduce the amount of code required to create <strong>and</strong> initialize an object.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!