Your First ASP Page

The following example is the "Hello World" example. We will be sending a simple string to display in Flash. The first step is to create the ASP page, so here is the code:

 <%@Language=VBScript%> <%Option Explicit%> <%   Response.Write("Hello World!!") %> 

If you were to test this script right now in a browser, it would display the text as it is, but from the previous chapter you learned that Flash will need this data in name/value pair format. So change the Response.Write line to the following:

 Response.Write("message=Hello World!!") 

Save the script as helloWorld.asp and put it on the server. Next up is the Flash part, so open Flash and follow these steps:


Create a new Flash document and save it as helloWorld.fla.


In the Actions panel of the first frame, place this code:

 //create the text field this.createTextField("display_txt",1,0,0,100,20); //the LoadVars object for loading the data var hello_lv:LoadVars = new LoadVars(); //when the LoadVars object receives the data hello_lv.onLoad = function(success){     if(success){         display_txt.text = this.message;     }else{         trace("An error has occurred.");     } } //load the asp hello_lv.load("http://MYSERVER/helloWorld.asp"); 

The first thing that happens in the preceding code is that a text field is created to display the message being returned. Next up is the LoadVars object, which will connect to the ASP page to get the message. After that, we set up the onLoad event callback for when the data is received. In it, we pass the parameter success to make sure there were no problems connecting to the ASP page. If success is true, the message is displayed in the text field we created; if not, an error is sent to the Output panel. Finally, we call the load method to get the data by passing it the full path to ASP page. If you were to put this on a server, you would not need the full path to the script, just a relative path.

Test the movie, and you should see a result similar to Figure 21.2.

Figure 21.2. The results of your first integration between Flash and ASP.


Wherever you see MYSERVER in the script, it is referring to the path to your server where the ASP pages reside.

That was a good example of connecting to an ASP page on a server and getting data back to Flash, but it is still not truly dynamic because we hard-coded the string to be sent back. The next example uses the built-in Now function in ASP to send back the date and time on the server.


When something is referred to as hard-coded, it means that the particular piece of code being referred to cannot be changed by outside forces, such as interaction with users or database information. It will stay exactly as the programmer coded it.

For this example, you need a new ASP page, so here is the code:

 <%@Language=VBScript%> <%Option Explicit%> <%   Response.Write("stamp=" & Now()) %> 

Similar to the previous example, this example sends back a single name/value pair to Flash. This time, however, it uses the Now function to get the current time and date on the server. Also notice that an ampersand (&) is used to combine strings in VBScript. Save this script as geTDateStamp.asp and put it on your server. If you like, you can test the page to see what is being returned by simply mapping to it with a browser.

Now for the Flash part:


Create a new Flash document called getServerDate.fla.


Select the first frame, open the Actions panel, and then place these actions in it:

 //create the text field this.createTextField("display_txt", 1, 0,0,200,40); //set a few properties of text field display_txt.html = true; display_txt.multiline = true; //the LoadVars object for getting the data var stamp_lv:LoadVars = new LoadVars(); //for when the LoadVars object receives data stamp_lv.onLoad = function(success){     if(success){         display_txt.htmlText = "The time on the server is:<br>";         display_txt.htmlText += "<b>" + this.stamp + "</b>";     }else{         trace("There was an error connecting, try again.");     } } //get the data stamp_lv.load("http://MYSERVER/getDateStamp.asp"); 

This code is very similar to the previous example, except we mixed it up a bit by setting the html property to true and the multiline property to true so it will display a little differently. Test it out and it should appear like Figure 21.3 but with a different date and time.

Figure 21.3. The first example of using ASP and Flash to create truly dynamic content.

It was not a lot of data, but it was dynamic, and you can tell because every time you test the Flash file, it will display different information.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: