Data Binding
Data binding is a feature introduced back in Flash MX 2004 Professional. It allows you to tie properties of components to one another that will automatically update when certain component events fire. It makes creating web-service-driven applications that much easier because there is very little code required. In fact, you will see that the preceding example can be done in a single line of code.
1. | Return to the stage and open up the Actions panel in the first frame of the actions layer. Remove all but the last line, so the code should now read like this:
|
2. | Now select the WebServiceConnector component, go to the Component Inspector panel (Window, Component Inspector), and select the Binding tab. |
3. | Select the Add Binding button, and the panel should then look like Figure 25.7. Choose results:String and click OK.Figure 25.7. The Add Binding dialog box used to add data bindings between components for fast development.![]() |
4. | Now you will see the "results" data binding in the Component Inspector panel. Select it and click in the bound to field. |
5. | When you do this, a magnifying glass will appear at the end of the field. Select it, and the Bound To dialog box appears as shown in Figure 25.8. (You can also double-click this field to get the same result.)Figure 25.8. The Bound To dialog box.![]() |
6. | Select the TextInput component in the left window and click OK. |
Now test the movie again.Again, you get the same results as before, but the difference is that the only ActionScript in this file is the line that activates the webServiceConnector component.Now that we are interacting successfully with our web service and have found the easiest way to work with them, let's actually make a useful web service.In the following example, you will create a web service that accepts a parameter, then squares it, and returns the results.Open a text editor, create a new file and enter the following code, and then save the file to your web root directory as squareService.asmx.
Much like our previous web service, this one starts off by declaring that it is a web service written in C#. It then grabs the object classes we need. Next, it declares the web service class. After that, we declare the web method and set its description. Then we create the web method. Notice we declare that the result being returned will be an integer. And the parameter being sent will also be an integer.Save this file and then browse to it and choose the squareNum web method. You will see a screen like Figure 25.9. As you can see, the web service has provided a field where we can place a number to test, so enter the number 8 and click Invoke. You should then see another browser screen like Figure 25.10, indicating that it worked.
<%@ WebService Language="c#" debug="true" %>
using System.IO;
using System.Web.Services;
public class Square: System.Web.Services.WebService{
[WebMethod(Description="Square the number")]
public int squareNum(int sentNum) {
int myReturn = sentNum*sentNum;
return myReturn;
}
}
Figure 25.9. The web service provides a field to test the web method.
[View full size image]

Figure 25.10. Use parameters in web methods to make web services work for you.
[View full size image]

The preceding code is nothing new; we create the event listener object. Then we set the event to the listener that will trigger the webServiceConnector to activate. And finally, we add the event listener to the Button component.