Displaying an alert pop-up dialog box from a component
Issue
Using an alert pop-up dialog box is a common practice to display a quick and easy dialog box in a Macromedia Flex application. However, you will get a compilation error if you try to use it from a Flex component. This TechNote will explain why and show the proper approach to display the alert box from a component.
Usually, you can just use the "alert()" function from a Flex application using ActionScript to display the alert pop-up box, as shown below:
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" ><mx:Button label="Alert On the Main" click="alert('Alert Box from the Main page')" /> ...</mx:Application>
However, if you use the same function from a Flex component (any MXML file that does not contain the<mx:Application> tag), you will get a compilation error, similar to the following:
There is no method with the name 'alert'.
Reason
The reason the compilation error occurs is that thealert function is a static method of theApplication object and isn't available from any Flex components. For a component, you must use amx.controls.Alert object and call theshow() method to display the dialog box. Since theshow() method is a static method, you don't have to instantiate the Alert object first.
Solution
The code below demonstrates the proper way to display an alert box from a component:
<?xml version="1.0" encoding="utf-8"?><mx:VBox xmlns:mx="http://www.macromedia.com/2003/mxml" ><mx:Button label="Show Alert from the Component " click="mx.controls.Alert.show('Alert Box from the Component')" /></mx:VBox>
This content requires Flash
To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.
Download the free Flash Player now!
