ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash


 Top ASP 101 Stories Top ASP 101 Stories
Migrating to ASP.NET
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @ Microsoft.com

QUICK TIP:
Don't touch that mouse!
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Flash and ASP Integration

by Dan Waters

ASP and Flash? How?

Macromedia's Flash and Microsoft's Active Server Pages technologies are two products with an extremely heavy impact on the world of web site development. Flash allows you to create zippy, vector-based animation and interactivity in a small ActiveX control, and ASP allows you to create dynamic HTML content on the fly. Hopefully, by the time you're done skimming this article, you'll be confident enough to use ASP in the creation of zippy, vector-based Flash content on the fly.

To many developers, a site done in Flash is worth many done in HTML. The ease of use and graphical appeal of Flash is unmatched. Where many of us get stuck, however, is at the question "How am I going to get data into Flash from an external source?" The answer to this question is also the key to a frighteningly successful web site.

The Concept

The first thing you need to know about Flash is the method in which it handles variables from remote files. Flash will take an URL-encoded query string and transform it into a list of variables within its own memory. An example of a string that Flash recognizes as variable definitions looks like this:

size=Medium&color=Navy+Blue&style=Mandarin+Collar

When you import this string into Flash, you will have three new variables: size, color, and style. These variables will already be initialized; loaded with the data you passed in the string.

size Medium
color Navy Blue
style Mandarin Collar

Be aware that Flash is incredibly easy to please as long as you employ the Server.URLEncode method. Flash does all the decoding itself. In the same respect, when Flash sends variables to a script, they will already be URL-encoded.

Techniques

Returning a valid string from ASP

The code below will return a valid string to the Flash movie.


<%@Language="VBScript"%>
<%
    Option Explicit
    Dim var(3), i, count

    i = 0
    count = 3

    var(0) = "Winken"
    var(1) = "Blinken"
    var(2) = "Nod"

    Do While i < count
        Response.Write "var" & i & "=" & var(i) & "&"
        i = i + 1
    Loop

    Response.Write "i=" & i
%>

When you execute this program, ASP returns this string:

var0=Winken&var1=Blinken&var2=Nod&i=3

Passing values from ASP to Flash

Remember that query string I mentioned above? You'll be using this format with ASP next. To pass variables from ASP to Flash, you must write a string back to the response in the above format.

To load variables from an ASP file, you should use the following action script:

Load Variables ("myscript.asp", 0)

( The Load Variables command can be found by choosing Load/Unload Movie from the dropdown list and selecting the Load Variables Into Location radio button. )

Generally, you should not use Get URL unless you are a) rewriting the object code to embed a new movie or b) generate HTML in a new page.

The zero in the above script represents the level at which you will load the movie. If you have movies stacked upon one another, you should reference them by their z-order. _level0 is the first movie, _level1 is the movie stacked above the first, and so on. For our current purpose, we'll assume there is only one movie in the picture and use 0 for the level specification.

Passing values from Flash to ASP

Surely you will want to send out form data from Flash to ASP. To send variables from Flash to an ASP program, use the following ActionScript code:

Load Variables ("myscript.asp", 0, vars=POST)

When you send variables using POST, you can access them in your ASP script using one of two methods:

  1. Request.Form("flash_var_1")
  2. Request("flash_var_1")

That's right... when you POST variables, Flash sends them in the form collection.

When you send Flash variables using GET, however, they are written into the query string, and subsequently you should use Request.QueryString("flash_var_1").

When you specify "Don't send" as a destination for the variables, Flash assumes you're loading data from the ASP file. You can use a query string parameter for the URL value of the Load Variables call and still retain the "don't send" variable transmission specification to avoid overwriting variable values.

Exercise: Transferring Values
From Flash to ASP to a Database to ASP to Flash

Download support materials for Exercise 1 from here.

View the code here.

The files are named as such:

employee.mdb Access database
employee.asp ASP script
employee.fla Flash file
employee.swf Flash movie
employee.html Movie mounting page

We will be employing ADO database routines in this example to make all this useful to you in the future. I have created a table called Employees in a file called employee.mdb which resides in the same directory as the script. The table looks like the one below:

ID NameLast NameFirst Position
1 SMITH JOHN CEO
2 BROWN SUSAN MARKETING DIRECTOR
3 STANFORD ROBERT SALES REPRESENTATIVE

That's it; three simple records. Notice how all the data is in capital letters: In a real-world application, this would really only be relevant to the fields upon which you would search. Putting the data in capital (or lowercase) letters guards against case mismatch in searches, because we can UCase the data from Flash before sending it to ADO.

Save your table as Employees and save your database as employee.mdb in a directory by itself.

Next, we'll make the input screen in Flash.

Open up Flash or use the support materials available.

The Data Entry Frame

In the first frame, create a screen with a text label that reads "Please enter employee's last name." Next to it, create a text field (push the |ab button down to make it an editable field) and right-click on it. Go to Properties, and where it says Variable, enter NameLast. You should go to the first frame's Properties and add an action script that simply says "Stop." This prevents the movie from advancing until you have entered a value and clicked Submit.

The Submit Button

Create a button that says Submit. Then drop it onto the stage and go to its Instance Properties by double clicking it. In the Actions tab, enter this action script:
Go To and Play (2)

This sends the user to the second frame.

Loading The Variables Behind The Scenes

Create, in the second frame, a blank keyframe with one frame action:

Load Variables ("employee.asp", 0, vars=GET)

The "Loading Data" Loop

In the third frame, you will want to create a short animation that tells the user that ASP is loading data. In this case, I used frames 3-8 for this animation. At frame 3, I entered the following frame action:

If (Position ne "")
   Go To and Stop (9)
End If

This checks to make sure that ADO has pulled our last variable, Position, from the database and returned it to the response. Otherwise it continues to play the Loading sequence until frame 5, at which we have this action:

Go To and Play (3)

This causes the animation to play over and over again and continue to check the value of Position. When ASP is finished, the animation will automatically go to frame 6.

The Result Frame

At frame 6, we should have a small text label that reads "ASP has returned the following results." Below this text label, we should have three more text labels in a row which read "Last Name", "First Name", and "Position". These are captions for our editable text fields below. Insert editable text fields (click the |ab button on the toolbar when you drop it, just like in the first frame). Set one's Variable equal to "NameLast", set another one's equal to "NameFirst", and the last one's equal to "Position" (all without the quotation marks).

Publishing

Save your document file as employee.fla in the same directory as your database.

Then, go to File menu -> Publish, and you will find employee.swf and employee.html residing in the same directory you saved employee.fla.

Okay, that's it, you're done with the Flash side of it. Now it's time to move on to ASP.

Open up your favorite ASP Editor (Mine is Textpad!), such as InterDev or even Notepad. You will be using an ADODB recordset and connection with a SQL connection string. You will not return anything to the response except the URL-encoded variable definition string. Are you ready? Well, get that way, because here's the code.


<%@Language="VBScript"%>
<%
    Option Explicit     ' Don't ever let me catch you without this line!

    Dim oRS, oConn      ' Recordset and connection objects

    Set oConn = Server.CreateObject("ADODB.Connection")
    oConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
        "DBQ=" & Server.MapPath("employees.mdb")
    oConn.Open

    Set oRS = Server.CreateObject("ADODB.Recordset")
    oRS.Open "SELECT * FROM Employees", oConn, 2, 3

    ' The next line looks for the specified name and UCases the
    ' last name we searched for to avoid case-sensitive issues.
    oRS.Find "NameLast = '" & UCase(Request.QueryString("NameLast")) & "'"

    ' If the last name does not exist, then return Not Found to the response.
    ' Otherwise return appropriate variables.
    If oRS.EOF Then
        Response.Write "NameLast=Not+Found&NameFirst=Not+Found" & _
            "&Position=Not+Found"
    Else
        Response.Write "NameFirst=" & Server.URLEncode(oRS("NameFirst")) & _
            "&NameLast=" & Server.URLEncode(oRS("NameLast")) & _
            "&Position=" & Server.URLEncode(oRS("Position"))
    End If

    ' Clean up and say goodbye.
    oRS.Close
    Set oRS = Nothing
    oConn.Close
    Set oConn = Nothing
%>

Save this code as employee.asp

Now, access employee.html through the local intranet. You should, if you haven't already, stick all these files somewhere in your web root and access it via http://localhost/employee/employee.html (or wherever you have stored the files). Since ASP is processed server-side, you cannot access employee.html via the hard drive and expect values from the ASP application. This means you will have an endless "Loading" loop

Once you have pulled up the page on the Intranet, enter a name which you know is in the database, such as Smith. ASP will return the values to Flash, and you will see the results when ADO is done processing.

Good Luck with this! If you have troubles, please contact me at dan@catapultic.com.

-Dan


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES