Asp. NET to achieve the tight integration of flash and. Net

Source: Internet
Author: User
Tags exception handling implement connect new features variables variable xml parser domain
asp.net

Introduction Macromedia Company's Flash is now widely used on the Internet to achieve enhanced content description of Web sites. Macromedia's recently released Flash (Flash 6 and Flash MX) version has made it clear that Macromedia is seriously considering taking Flash as the interface option for Web application development. Flash may be a great choice for an interface, however, this option is not used to replace business 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 joint Microsoft's. NET technology.

Flash includes actionscript-, a scripting language used to add interactivity to animations. Since the introduction of the ActionScript script from Flash 5, Macromedia has constantly improved it from a simple scripting language to a fairly strong programming feature that still has many limitations. In Flash MX 2004, Macromedia introduces ActionScript 2.0-it supports new features and new ways to build code. To make it easier to understand this article, you should first read some basic knowledge about using ActionScript to build a flash site.

Macromedia has introduced methods to enable easy integration of external data into flash applications to allow more complex and robust data-driven applications. Both Flash and. NET recommend themselves as Cross-platform solutions and, as a result, a natural partner. Still, joining these two technologies is not a simple task. This article will discuss several ways to integrate flash and. NET applications together with you.

Second, integrated flash and asp.net

Embed flash animations into asp.net pages

Macromedia Flash animations are placed in the. aspx page by using the Federated object and embed labels. The embed label was originally developed by Netscape for the purpose of working with Netscape 2.0 or more advanced versions. Later, the label was adopted by Microsoft's Internet Explorer (MSIE). To achieve browser compatibility, you can use MSIE specific object tags and embed tags together. The following HTML code shows the case of embedding a Flash animation (*.swf file) with the object label and the embed tag:

  Third, to connect flash to external data there are three ways to integrate Flash animation with external data: Flashvars,loadvars and new flashremoting technology. One of the easiest ways to do this is to pass the variables in the HTML part of the code by using Flashvars to deliver the external data from an. aspx page to a flash animation embedded in a Web page. These variable values set with Flashvars can be accessed at the root level of the Flash animation.  To be able to run in all browser environments, the Flashvars property must be assigned in the object and embed tags. The following is the Flashvars:<param name= "Flashvars" value= "Init=yes&check=true" with an object tag, which is used with EMBED tags flashvars:<embed Src= "http://www.pushad.com/Info/test.swf" flashvars= "init=yes&check=true" > </EMBED> to learn more about flashvars information,  Please refer to the article http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_16417 from Macromedia. Four, why use flashvars do not borrow flashvars, you can also put the variable in the HTML tag-through a query string to add it to the. swf file after the URL, and then passed to a flash animation. There may be a problem with this method-if the variable is in the. The SWF file is loaded when it is needed because of one. The SWF file should be downloaded and run before the request data is sent from the server. Variable values may also be set through the "Load Variables" method and JavaScript in Flash. Each of these methods has the disadvantage of lazy-slow response; This is why developers should use Flashvars to pass data from asp.net to flash animations. The following figure shows code that passes a variable in a query string: Any variable that uses flashvars can be accessed at the root level (_level0) of the Flash animation. To retrieve these values within a flash animation, you can create a dynamic variable. The following ActionScript code implementation in Flash for "InitValue" Assigned value: _level0.inItvalue = init; V. Flash and database communication a flash animation cannot directly access a database. However, Flash can communicate with the database via any server-side technology, such as ASP or asp.net. There are several ways to accomplish this task.  In this article, we will explain the Loadvariables method and the Sendandload method. (i) using the Loadvariables method in cartoon breaks the Loadvariables method reads data from an external file or URL and sets a value for the variable that is broken by the animation. Once the cartoon break is instantiated, the animated load action in the Fragment event event handler is initialized. When the animated cartoon is initialized, the Loadvariables method can be called to load the variable. The Loadvariables method is an asynchronous call and Flash does not wait for the result of the call.  When data is received in a loadvariables () behavior, the animated data behavior in the Fragment event event handler is initialized. To illustrate the use of this method, this example retrieves the user's name and age from a database based on the ID selected from the Flash animation. In the following illustration, the Loadvariables method of the animation is used to load data into an animated fragment from an. aspx page getuserdata.aspx. Here, ActionScript passes the user's id-his name and age will be displayed in a Flash animation in the form of a query string to the. aspx page.

This getuserdata.aspx page retrieves the ID value from the query string shown below:


Now, ASP. NET can pass the ID of this value to the. NET business component. This example retrieves the user's name and age:


As mentioned earlier in this section, the data behavior of the animated Onclipevent event handler is initialized when the data is received in a loadvariables () behavior. Declare the following dynamic variables in the Flash Animation: Flashname and Flashage. The following figure shows the ActionScript code-it is used in the Onclipevent processor of the Flash animation to retrieve the values returned from the. aspx page.

Note: It is 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"-the variable set from the. aspx page is the same (Response.Write ("Name=" &user.name)).


These values can now be used inside the Flash animation.

(ii) Use of Sendandload method from cartoon break

on today's Internet, XML is an important form of a document system as a universal transmission format with the potential to successfully exchange data between different types of applications. XML provides a gateway mechanism for communication 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 parser, they can share very structured information that is very easy to handle.

Flash MX supports XML data transfer and has several ASP and ASP.net server technology methods to implement the exchange of XML. Flash MX can send and receive XML packets by using the new Xmlconnector of XML classes-load,send and Sendandload or Flash MX. The last one can be used to send and receive packets over HTTP and bind them to the flash layer on the UI part.

Let's discuss the sendandload approach to the Flashxml object, which is a more popular way to implement schema-oriented XML data communications, exception handling, and performance.  We use this simple example: a flash client inserts the details of a user's score into the database and sends a successful or failed message to the Flash client.   (iii) Create an XML package in Flash an XML packet can be easily created in Flash, as follows: var sendxml:xml = new XML ("<userid> 123456 </userid>"); Send an XML package to a server-side page-it uses asp/asp.  NET and processing returns an XML packet. The usage is as follows: Xmlobject.sendandload (url:string, targetxmlobject:xml): Void This ' Xmlobject ' is an XML object instance-it encapsulates the XML that must be sent to the. aspx page;  URL ' is the destination URL for the specified XML object; ' Targetxmlobject ' is the XML object-it will receive the information returned from the server side. The Sendandload method sends an XML packet to a server-side template and receives an XML packet as a response-unlike the Send method of an XML class-it sends only one XML package without expecting any type of response. Each of these methods works in its own way, but there is one important difference: The Send method of the XML class takes a target as an argument-it allows you to open a new browser window or replace the contents of the current browser window. The Sendandload method takes an XML package as an argument-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-which can be extremely useful for debugging. Note: If the flash file that emits the Sendandload call runs in a Web browser, the target URL will be in the same domain as the Flash animation.  If you also need to communicate with an application in a different domain, refer to the "Cross domain Data Loading" on the www.macromedia.com station for more information. The code in the following graphic shows how the Sendandload method of an XML object is used to communicate with an. aspx page and how to handle XML returned from the server:
The above code does the following: • Load an XML string into an XML object "Userscore_xml". The instance has xml-it is sent to the remote server-side asp.net page. • Sets the Ignorewhite property of the XML object "Responsexmlobj_xml". This XML object instance holds XML packets from the server. • Invoke the ("Userscore_xml") Sendandload method of the XML object to send the XML to the server-side. aspx page, http://localhost/MyTestApp/ Scoring.aspx and stores the returned XML packets into the XML object instance "Responsexmlobj_xml". • Set the OnLoad method of the XML object "Responsexmlobj_xml".  Flash receives the XML response from the server-side. aspx page-it is processed in the OnLoad method of the XML object. • It then analyzes the received XML packets to display the necessary messages on the Flash client. (iv) Receive XML packets sent from Flash in the ASPX page the following graphic shows the code to complete the task: The above code completes the following: • Receive the XML sent from the Flash client (Xmldoc.load (Request.inputstream)). Analyze the received XML and pass it to the business component to insert into the database. • form an XML message to be routed back to the Flash client. The success or exception conditions raised in the business layer are processed accordingly and converted into various XML messages-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 marketplace today and will likely become successful partners in the growing application architecture. These two techniques deal with some of the same problems, but are essentially different. Flash MX enables Web sites to experience interactivity, while. NET helps build enterprise application software. As explored in this article, it is entirely possible to integrate flash and. NET applications.  Each step is made in a decision-how to connect the two parts and strategically plan the right architecture. Although the next generation architecture around Flashremoting is gaining momentum in this area, the approach discussed in this article will be applied to large areas in a few years.

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.