Home / AJAX

HOW TO Instantiate Controls Imperatively

Modified on 2009/12/04 17:12 by Stephen Walther Categorized as Uncategorized
If you are comfortable programming with JavaScript then you will find the imperative approach to instantiating Ajax Library controls to be the most intuitive approach. When using an imperative approach, you instantiate a control by calling the Sys.create method. For example, in the following page, a Watermark control is created and attached to an input element in the HTML markup.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <title>Simple Watermark</title>

<script src="http://ajax.microsoft.com/ajax/beta/0911/start.debug.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/beta/0911/Extended/ExtendedControls.debug.js" type="text/javascript"></script>

<script type="text/javascript">

Sys.require(Sys.components.watermark, function() {

Sys.create.watermark("#input1", "Enter something...");

}); </script> </head> <body>

<input id="input1" />

</body> </html>

Notice that the Sys.require() method is used to load all of the scripts required by the Watermark control (The Sys.require() method uses the Script Loader in the background).

Notice that the page includes an explicit reference to ExtendedControls.debug.js. If you include this file, and you use Visual Studio/Visual Web Developer, then you get full Intellisense when calling the Sys.create method.

  Name Size
- ImperativeIntellisense.png 26.41 KB