ASP Net and Flash Communication

In this article you will learn 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.

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 entire ‘object’ tag and paste it inside tag of ‘Default.aspx’ file.

After pasting the above code little changed needed on ‘AspFlash.swf’ parameter like the following. Look at the line ‘AspFlash.swf?mydata=”’ what we added. Flash read _urlĀ­ data with mydata which will be supplied by ASP.Net later.

Finally, add two ASP.net standard controls on ‘Default.aspx’ page i.e. TextBox and Button. Change Button text property to ‘Send Data’.

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.

Both comments and pings are currently closed.

Comments are closed.

Bad Behavior has blocked 1311 access attempts in the last 7 days.