- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
ASP.Net and Flash Communication
Hello readers, I've covered in this article is just a small and simple overview of the world of Flash development with ASP.NET. I have recently designed a website, which thoroughly covers all of the Flash to ASP.NET communication methods mentioned in this article, as well as a step-by-step introduction to ASP.NET development with C# using Visual Studio.NET coolest IDE and Adobe Flash CS.
Step 1
Open Adobe Flash CS. Create a new document selecting Flash File (Action Script 2.0). You may be interested in Action Script 3 (AS3) but I choose Action Script 2 (AS2) for easier understand. Just step with me and I assure you, you will become a good Flash developer after reading this article. Now you will see a single tab namely ‘Untitled 1’ in the Adobe Flash. After saving the file ‘Untitled 1’ text will replace with your preferred filename. I named it ‘AspFlash.fla’. Remember FLA is a flash source file and your output movie will be SWF, which will need to be embedded in ASP.Net ASPX file later. Adobe Flash split-down with several window, do not confused. You do not need to know the all window features. Start with left called ‘Tools’, in the center top window called ‘Timeline’, next down window called ‘Scene’, next bottom window called ‘Properties’ and the right most window split-down with many window ‘Color’, ‘Align’, ‘Components’ and ‘Library’. Those entire windows can be switched on/off by ‘Window’ menu. Look at the ‘Scene’ window which will be your design area. From the ‘’Properties’ window you can change colors and size as per your requirements.
Step 2
Now add some component from the ‘Components’ window expand ‘User Interface’. Oh! lots of stuff. Drag only one ‘TextInput’ and one ‘Button’ on your ‘Scene’ window and align them correctly. Select ‘TextInput’ and put an instance name (e.g. TextInput1) from ‘Properties’ window. Without instance name, Action Script will not recognize any components. Do same for the ‘Button’ instance name (e.g. SendData) and from the ‘Parameters’ tab change ‘Button’ label (e.g. Send Data).
Step 3
Here we start out main coding part. Select ‘Layer 1’ from ‘Timeline’ window and press F9 (keyboard function key). You will see ‘Actions’ window, where you writes you’re AS code. Type or copy pest the following codes.
SendData.onPress = function() {
//Declare and Initialize variable
var send_lv:LoadVars = new LoadVars();
//Assigning value to parameter, like Asp.Net QueryString
send_lv.mydata = TextInput1.text;
//Sending data
send_lv.send('default.aspx', '_self', 'GET');
};
The LoadVars object is used for exchanging data between flash – server. The LoadVars object is capable of either sending data to the server for processing, loading data from the server, or sending data to the server and waiting for a response back from the server in one operation. The LoadVars object uses name-value pairs to exchange data between the client and the server. The LoadVars object is best used in a scenario that requires two-way communication between the Flash Movie and server-side logic, but doesn't require large amounts of data to be passed.
Send method sends the variables in the send_lv object to the specified URL. The string is posted to the URL using the HTTP
GET
method, so that ASP.Net easily read mydata variable in QueryString.
Step 4
Type or copy pest the following codes to read the QueryString in Flash – Action Script 2. In Action Script 2 there are no methods like ASP.Net provides, so I wrote the following codes to get QueryString from URL. The _url method returns the URL of the ‘AspFlash.swf’ file that was loaded with ASPX page.
//Reading QuaryString
myURL = this._url;
myPos = myURL.lastIndexOf("?");
if (myPos > 0) {
var myRawParam = myURL.substring(myPos + length('mydata=') + 1, myURL.length);
myParam = myRawParam.toString().split("'").join("");
if (myParam != undefined){
TextInput1.text = myParam;
}
}
Step 5
Save your file from File menu. Now we need to make the final SWF move and embed it to ASPX page. From File menu click ‘Publish Settings’ and you will see a new window containing three tabs (Formats, Flash and HTML). In the Formats tab check Flash and HTML types, so that you can get the SWF embedded code in HTML page. Now press button ‘Publish’ to build the final move. If there are no error occurred, flash will provide you to two files (e.g. ‘AspFlash.swf’ and ‘AspFlash.html’) in root folder where source file ‘AspFlash.fla’ located.
Step 6
Now start Visual Studio .Net (VS) and create a new website and name it ‘AspFlash’. VS create a default page namely ‘Default.aspx’. From solution explorer double click on ‘Default.aspx’ file to view Markup code (also called Inline code) like following.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Now copy ‘AspFlash.swf’ and ‘AspFlash.html’ files in to your web root directory. I mean ASPX, SWF files should be located in same directory. Open ‘AspFlash.html’ file and copy the following lines and paste it inside <div> </div> tag of ‘Default.aspx’ file.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="AspFlash" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="AspFlash.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /> <embed src="AspFlash.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="AspFlash" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
After pasting the above code little changed needed on ‘AspFlash.swf’ parameter like the following. Look at the line ‘AspFlash.swf?mydata='<% =Request["mydata"] %>'’ what we added. Flash read _url data with mydata which will be supplied by ASP.Net later.
<div>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="550" height="400" id="AspFlash" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="AspFlash.swf?mydata='<% =Request["mydata"] %>'" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="AspFlash.swf?mydata='<% =Request["mydata"] %>'" quality="high" bgcolor="#ffffff"
width="550" height="400" name="AspFlash" align="middle" allowscriptaccess="sameDomain"
allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</div>
Finally, add two ASP.net standard controls on ‘Default.aspx’ page (e.g. TextBox and Button). Change Button text property to ‘Send Data’. The full ‘Default.aspx’ will looks like the following.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="550" height="400" id="AspFlash" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="AspFlash.swf?mydata='<% =Request["mydata"] %>'" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="AspFlash.swf?mydata='<% =Request["mydata"] %>'" quality="high" bgcolor="#ffffff"
width="550" height="400" name="AspFlash" align="middle" allowscriptaccess="sameDomain"
allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</div>
<div>
<asp:TextBox ID="textbox1" runat="server" Text=""></asp:TextBox>
<asp:Button ID="button1" runat="server" Text="Send Data" OnClick="button1_Click">
</asp:Button>
</div>
</form>
</body>
</html>
Step 7
In this step you need to open ‘Default.cs’ file by clicking ‘View Code’ pointing on ‘Default.aspx’ from Solution Explorer of VS. By default VS added Page_Load event procedure. You need to add some text on Page_Load event procedure along with button1_click event procedure like the following.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
if (Request["mydata"] != null)
textbox1.Text = Request["mydata"].ToString();
}
protected void button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/default.aspx?mydata=" + textbox1.Text);
}
Step 8
Now build the website using F5 (keyboard function key) and type some text in Flash movie and click ‘Send Data’ to send Flash data to ASPX page. You will see ASPX ‘TextBox’ text changed with your Flash ‘TextInput’ text.
Same way type some text in ASPX ‘TextBox’ and click ‘Send Data’ Button to send ASPX data to Flash movie.
Enjoy the communication technique between ASP.Net and Flash. If need further assistance, feel free to contact me via email.