How to Get a reference to a control through JavaScript

[Infragistics] Tony Lombardo / Saturday, February 28, 2009

The following code can be used to get a reference to an Infragistics control through JavaScript.  Note ClientID should be used, this value can be retrieved through the "ClientID" property of the control.  Here's a simple example, where "webdatagrid" is the ClientID of the target control.

function pageLoad() 
{ 
	//Call $find with the ClientID of the target control 
	var control = $find("webdatagrid"); 
} 

The code above works fine, but is rigid since you're hard coding a ClientID (something that can easily change in the future).  A slight modification will create a dynamic approach that will save you headaches in the future.

function pageLoad() 
{ 
	//A slight modification can be made to avoid hard-coding the ClientID 
	var control = $find('<%= WebDataGrid1.ClientID %>'); 
}

Placing the code above in your ASPX page will set the Javascript variable "control" to a client-side reference of the control "WebDataGrid1".  The <%= %> is a server-side code block (or scriptlet) which when evaluated will emit the result as a string into the HTML output.

I recommend using this modified "dynamic" method over the static one above it.  Incase you ever change your control containership, you won't have a hard-coded ID that would require manual updating.