Monday, December 5, 2011

$get and $find ASP.NET AJAX Shortcut Functions

Sys.UI.DomElement $get Method

Provides a shortcut to the getElementById method of the Sys.UI.DomElement class. This member is static and can be invoked without creating an instance of the class. The $get shortcut function points to the Sys.UI.DomElement.getElementById JavaScript function which is defined as part of the ASP.NET AJAX client side library (which means you will need to include a ScriptManager on the page to be able to use it).

<script type="text/javascript">
function changeColor(color)
{
// fetch the div
var div = $get('<%= label1.ClientID %>');
// set the color to the provided value
div.style.color = color;
}
</script>


Sys.Application $find Method

Provides a shortcut to the findComponent method of the Sys.Application class. This member is static and can be invoked without creating an instance of the class. Use $find to Fetch an AjaxControlToolkit Extender Behavior as well.

<script type="text/javascript">
function checkComponent()
{
if (!($find('MyComponent', div1))) {
div1.innerHTML = 'MyComponent is not available.';
}
</script>

That's it. Well done!

No comments:

Post a Comment