Home / AJAX

HOW TO Instantiate Controls Declaratively

RSS
Modified on 2009/12/16 18:28 by Erik Reitan Categorized as Uncategorized
The ASP.NET Ajax Library also supports a purely imperative syntax for instantiating client controls. For example, the following page attaches a Watermark control to an input element.

<!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" >
<head>
    <title>Declarative Watermark</title>
    <script type="text/javascript" src="../Scripts/Start.js"></script>

<script src="http://ajax.microsoft.com/ajax/beta/0911/MicrosoftAjax.debug.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/beta/0911/MicrosoftAjaxTemplates.debug.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/beta/0911/extended/Common/Common.debug.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/beta/0911/extended/ExtenderBase/BaseScripts.debug.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/beta/0911/extended/TextboxWatermark/TextboxWatermark.debug.js" type="text/javascript"></script>

</head> <body xmlns:sys="javascript:Sys" xmlns:watermark="javascript:Sys.Extended.UI.TextBoxWatermarkBehavior">

<input id="input1" sys:attach="watermark" watermark:watermarktext="Enter some text" />

</body> </html>


Notice that two namespace prefixes are declared in the body element: sys and wm. Both of these namespace prefixes are used to add custom attributes to the input element in the body of the page.

The Watermark control is attached in the input element with the sys:attach attribute. The watermarktext property is set by assigning a value to the watermark:watermarktext attribute.