Close integration of flash and. Net in ASP. NET)

Source: Internet
Author: User

1. The flash feature determines that flash can be integrated.
Macromedia Flash is now widely used on the Internet to enhance the content description of web sites. Macromedia selects flash as the interface developed by the web application Program . Flash may be a great choice for an interface. However, it is not used to replace commercial logic, query management, and security. For a web developer, to achieve truly strong performance and enterprise-level capabilities, you need. net, Flash, and rich description layer support, and work with Microsoft's. NET technology.
flash includes ActionScript, a scripting language used to add interactivity to animations. Since the introduction of ActionScript scripts in Flash 5, Macromedia has continuously improved it, from a simple scripting language to a fairly solid programming feature with many limitations. In Flash MX 2004, Macromedia introduced ActionScript 2.0, which supports new functions and methods to build Code . To make it easier to understand this article, you should first read some basic knowledge about how to build a Flash site using ActionScript.
Macromedia has introduced methods to easily integrate external data into flash applications to allow more complex and robust data-driven applications. Flash and. Net both recommend them as cross-platform solutions, and thus become a natural combination. Still, joining these two technologies is not a simple task. This Article will discuss several methods to integrate flash and. NET applications.

2. Integrate flash and ASP. NET
Embed Flash animation into ASP. NET page
The Macromedia Flash Animation uses the object and embed labels to place them on the. ASPX page. The embed label was initially developed by Netscape to work with Netscape 2.0 or a more advanced version. Later, this label will be used by Microsoft Internet Explorer (MSIE. To achieve browser compatibility, MSIE-specific object tags and embed tags can be used together. The following HTML code shows how to embed a Flash Animation (*. SWF file) with the object tag and the embed tag:

3. Connect flash to external data
There are three ways to integrate Flash animation with external data: flashvars, loadvars and the new flashremoting technology. One of the simplest methods is to use flashvars to pass variables in the HTML section of the Code to extract external data from one. the ASPX page is passed to a flash animation embedded in the web page. These variable values set using flashvars can be accessed at the root level of the Flash animation. To run in all browser environments, the flashvars attribute must be assigned a value in the object and embed labels, as flows:

<Object codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 class =" Height = 390 width = 450 classid = CLSID: D27CDB6E-AE6D-11cf-96B8-444553540000>
<Param name = "_ CX" value = "11906"> <Param name = "_ CY" value = "10319">
<Param name = "flashvars" value = "">
<Param name = "movie" value = "../MAP/mapindex.swf"> <Param name = "src" value = "../MAP/mapindex/. SWF">
<Param name = "wmode" value = "Opaque">
<Param name = "play" value = "-1">
<Param name = "loop" value = "-1">
<Param name = "quality" value = "high">
<Param name = "salign" value = ""> <Param name = "menu" value = "-1"> <Param name = "base" value = ""> <Param name = "allowScriptAccess" value = ""> <Param name = "scale" value = "showall"> <Param name = "devicefont" value = "0"> <Param name = ""embedmovie" value = "0"> <Param name = "bgcolor" value = "00a000"> <Param name = "swremote" value = ""> <Param name = "moviedata "value =" "> <Param name =" seamlesstabbing "value =" 1 "> <Param name =" Profile "value =" 0 "> <Param name =" profileaddress "Value = ""> <Param name = "profileport" value = "0"> <Param name = "allownetworking" value = "all"> <Param name = "allowfullscreen" value =" false ">

<Embed src = ".. /MAP/mapindex.swf "width =" 450 "Height =" 390 "play =" true "loop =" true "wmode =" Opaque "Quality =" high "bgcolor =" Transparent" align = "" pluginspage = "http://www.macromedia.com/go/getflashplayer"> </embed>
</Object>

The flashvars with an object tag is as follows:

<Param name = "flashvars" value = "init = Yes & check = true">

 The flashvars used with the embed label is as follows:

<Embed src = "test.swf" flashvars = "init = Yes & check = true"> </embed>

4. Why flashvars?

If you do not use flashvars, you can also place the variable in the HTML Tag-add it to the URL of the. SWF file in the form of a query string, and then transfer it to a flash animation. This method may have a problem-if this variable is required when the. SWF file is loaded, because a. SWF file should be downloaded and run before the request data is sent from the server. The variable value may also be set in flash through the "load variables" method and JavaScript. Each of these methods has the disadvantage of being cool and slow; that is why developers should use flashvars to transfer data from ASP. NET to Flash animation. Any variables passed using flashvars can be accessed at the root level (_ level0) of the Flash animation. To retrieve these values in a flash animation, you can create a dynamic variable. The following ActionScript Code assigns a value to "initvalue" in flash:
_ Level0.initvalue = Init;

V. Communication between flash and databases

A flash animation cannot directly access a database. However, flash can communicate with databases through any server technology such as ASP or ASP. NET. There are several ways to complete this task. In this article, we will explain the loadvariables and sendandload methods.

(1) Use the loadvariables method in an animation clip

The loadvariables method of an animation fragment reads data from an external file or URL and sets the value for the variable of the animation fragment. Once the animation clip is instantiated, the loading action of the animation fragment in the clip event processor is initialized. When an animation clip is initialized, The loadvariables method can be called to load variables. The loadvariables method is an asynchronous call and Flash does not wait for the call result. When data is received in a loadvariables () action, the data behavior of the animation fragment in the fragment event processor is initialized.

To demonstrate the usage of this method, this example retrieves the user's name and age from a database based on the ID selected in the Flash animation. In the following figure, the loadvariables method of the animation is used to load data to an animation clip from getuserdata. aspx on the. ASPX page. In this example, the user ID passed by ActionScript-his name and age will be displayed in the Flash Animation in the form of a query string on the. ASPX page.


The getuserdata. ASPX page retrieves the id value from the query string displayed below:


Now, ASP. NET can pass the ID of this value to the. NET commercial component. In this example, the user's name and age are retrieved:


As mentioned earlier in this section, the data behavior of the onclipevent event processor of an animation clip is initialized and received when the data is in a loadvariables () action. Declare the following dynamic variables in Flash Animation: flashname and flashage. The following figure shows the ActionScript code-it is used by the onclipevent processor of Flash animation to retrieve the value returned from the. ASPX page.

Note: It is very important-it is the same as the variable set from the. ASPX page. In the example shown here, the variable accessed in the Flash animation is "name"-It is from. the variables set on the ASPX page are the same (response. write ("name =" & user. name )).


These values can now be used inside the Flash animation.

(2) Use the sendandload method from an animation clip

On the Internet today, XML, as a universal transmission format that can even allow smooth data exchange between different types of applications, plays an important role in the form of a document system. XML provides a gateway mechanism for communications between applications, even applications on a wide range of different systems. As long as applications can share data (via HTTP, file sharing, or another mechanism) and have an XML analyzer, they can share very easy-to-process structured information.

Flash MX supports XML data transmission and has several server technical methods like ASP and ASP. NET to implement XML exchange. Flash MX can send and receive XML data packets by using the new xmlconnector of XML class-load, send, sendandload or Flash MX. The last one can be used to send and receive packets over HTTP and bind them to the UI part of the flash layer.

Next we will discuss the sendandload method of the flashxml object. This is a popular method for implementing architecture-Oriented XML data communication, exception handling, and performance. Let's use this simple example: insert the details of a user's score from a flash client to the database and send a message of success or failure to the Flash client.

3) create an XML package in Flash

An XML data packet can be easily created in flash, as shown below:

VaR sendxml: xml = New XML ("<userid> 123456 </userid> ");
Send an XML package to a server page-it uses ASP/ASP. NET and processes XML data packets returned.

The usage is as follows:

Xmlobject. sendandload (URL: String, targetxmlobject: XML): void
This 'xmlobject 'is an XML object instance-it is encapsulated and must be sent. xml of the aspx page; this 'url' is the destination URL of the specified XML object; 'targetxmlobject 'is the XML Object-it will receive information returned from the server.

The sendandload method sends an XML package to a server-side template and receives an XML package as a response-different from the XML-class send method-it only sends an XML package and does not expect any type response. Each method plays a role in its own way, but there is an important difference: the send method of the XML class uses a target as the parameter-it allows you to open a new browser window or replace the content in the current browser window, the sendandload method uses an XML package as the parameter-it does not start a new browser window. Because the send method opens a new browser window, we can see the output of the browser window itself-this may be extremely useful for debugging.

Note: If the Flash file that sends a sendandload call runs in a Web browser, the target URL will be processed in the same domain as the Flash animation. If you still need to communicate with applications in different domains, you can refer to "Cross Domain data loading" on www.macromedia.com for more information.

The Code in the following figure shows how the sendandload method of an XML object is used to communicate with A. ASPX page and how the XML returned from the server is processed:


The above Code does the following:

· Load the XML string to an XML Object "userscore_xml ". The instance has XML-it is sent to the Remote Server ASP. NET page.

· Set the ignorewhite attribute of the XML Object "responsexmlobj_xml. The XML object instance holds the XML package from the server.

· Call the ("userscore_xml") sendandload method of the XML object to send the XML to the server. ASPX page, http: // localhost/mytestapp/scoring. and store the returned XML package to the XML object instance "responsexmlobj_xml.

· Set the onload method of the XML Object "responsexmlobj_xml. Flash receives the XML response from the server. ASPX page-it is processed in the onload method of the XML object.

· Then, it analyzes the received XML package to display necessary messages on the flash client.

(4) receiving XML packages sent from flash on the ASPX page

The following figure shows the code to complete this task:


The above code completes the following tasks:

· XML (xmldoc. Load (request. inputstream) sent from the flash client is received )).

· Analyze the received XML and pass it to commercial components for insertion into the database.

· XML messages are sent back to the Flash client. The success or exception conditions raised at the business layer are processed and converted to various XML messages accordingly-the message is sent back to the Flash client. The Flash client receives these XML messages and displays them accordingly.

Vi. Conclusion

Flash and. NET technologies are becoming increasingly popular in the market today and will likely become a successful partner in the ever-increasing application architecture. These two technologies handle some of the same issues, but they are actually different. Flash MX makes Web sites experience interactivity, while. Net helps build enterprise application software. As explored in this article, it is entirely possible to integrate flash and. NET applications. Every step is to make a decision-how to connect the two parts and make the right architecture planning strategically.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.