This section describes calling an ASPX page from Flash using Flash Remoting MX, working with the Flash Remoting custom server control in ASPX pages, using the Flash Remoting namespace in code-behind files, and so on. For more information on specific topics, see the following sections:
To call an ASP.NET (*.aspx) page from Flash Remoting MX, the ASPX page must reside within the directory or subdirectories of a Flash Remoting-enabled .NET application in the webroot.
Before calling an ASPX-based service from ActionScript in a Flash application, you must get a reference to the page.
#include "NetServices.as"
NetServices.setDefaultGatewayUrl("http://localhost/myASPApp/default.aspx");
The gateway URL must reference an ASPX page inside the application directory. The setDefaultGatewayURL function should only be used during development in the Flash MX authoring environment. When you deploy the Flash application, you should supply the gateway URL using a parameter in the HTML that embeds the SWF file in the web page.
gatewayConnection = NetServices.createGatewayConnection();
ASPXservice = gatewayConnnection.getService("myASPApp", this);
The first parameter specifies the directory that contains the ASPX page. The second parameter of the getService function, this, specifies that the result of service function calls are returned to this Flash timeline. For more information, see Chapter 2, "Handling service results".
Once you have a reference to the ASPX page, you can use ActionScript functions to invoke it. For example, the following ActionScript code invokes the ASPX page myASPPage.aspx, assuming that ASPXservice represents your reference to the directory that contains the ASPX page:
{
ASPXservice.myASPPage();
The ASPX page's file name, myASPPage.aspx, becomes the function name, myASPPage, in the reference to the page's directory structure.
To access data passed from or return results to Flash applications in ASPX pages, you use the Flash Remoting custom server control in your ASPX page. The Flash Remoting server control is provided by the flashgateway DLL, which is located in the local assembly cache (bin directory) of your application. Like any custom server control, you must first register it in your ASPX page, as the following example shows:
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
The Register directive establishes the tag prefix (Macromedia), namespace (FlashGateway), and the assembly that provides the functionality (flashgateway). After you register the custom server control in your ASPX page, you can use it to pass data to Flash applications, as the following example shows:
<Macromedia:Flash ID="Flash" runat="server">
Hello from .NET! </Macromedia:Flash>
When invoked from the Flash application, the string Hello from .NET! is returned.
In addition to passing simple strings, you can write code in a .NET-supported language that accesses parameters passed from Flash and returns processed results to Flash. The Flash Remoting custom server control contains two properties for accessing passed parameters and returning results: Flash.Params and Flash.Result.
The Flash.Params property is a list consisting of parameters passed from a Flash application. The parameters arrive in the order that they were passed from the service function call in the ActionScript code of a Flash application. The Flash.Result property returns its value to Flash.
You can access Flash parameters like any other value in .NET, as the following C# example shows:
<%@ Page Language="C#" debug="true" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<Macromedia:Flash ID="Flash" Runat="Server" />
<%
String message = "Hi ";
if (Flash.Params.Count > 0)
{
message += Flash.Params[0].ToString();
}
Flash.Result = message;
%>
Between the rendering blocks (<%...%>), the if statement condition, Flash.Params.Count > 0, evaluates the Flash.Params list for the number of parameters present. If a parameter is present, the parameter value, as a string, is appended to the message variable. Finally, the message variable is assigned into the Flash.Result property, which is returned to Flash.
If more than one parameter is passed from Flash, you access the parameters in your .NET application in the same order that they were passed from the Flash application. For example, the following ActionScript function passes two parameters, assuming firstname and lastname are input text fields in a Flash application:
ASPXservice.myASPPage(firstname.text, lastname.text);
In an ASPX page, for example, you access the parameters using strict array syntax, as the following VB.NET code shows:
<%@ Page language="vb" debug="true" CodeBehind="myASPPage.aspx.vb" AutoEventWireup="false" Inherits="myASPApp.myASPPage" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<Macromedia:Flash ID="Flash" Runat="Server" />
<%
dim message as string
message = "Hi "
if Flash.Params.Count > 0 then
message = message & Flash.Params(0).ToString() & " " & Flash.Params(1).ToString()
end if
Flash.Result = message
%>
In the code, the Flash.Params(0) property represents the firstname parameter, and the Flash.Params(1) variable represents the lastname parameter. The Page directive references a code-behind file, myASPPage.aspx.vb.
In ASP.NET applications, you can separate business logic from user interface code using code-behind files. In the code-behind files, you use the Flash Remoting namespace to access parameters from and return results to Flash. To use code-behind files, you use the codebehind property of the page directive in an ASPX page, as the following example shows:
<%@ Page Language="c#" Debug="true" codebehind="myASPPage.aspx.cs" autoeventwireup="false" Inherits="myASPApp.myASPPage" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %> <MACROMEDIA:FLASH id="Flash" Runat="Server" />
In the example, the page directive references the code-behind file in the codebehind property. The fully qualified class name is used in the Inherits property to inherit the methods of the code-behind file. You must also use the register directive to register the Flash Remoting custom server control, and then use the server control in the page.
In the code-behind file itself, you declare the Flash namespace as a protected variable in the class definition, as the following C# example shows:
namespace myASPApp
{
public class myASPPPage : System.Web.UI.Page
{
protected FlashGateway.Flash Flash;
...
}
}
The following VB.NET example performs the same operation:
Namespace Samples.ado
Public Class CustomerInfo
Inherits System.Web.UI.Page
Protected Flash As FlashGateway.Flash
After you establish the Flash Remoting namespace, you can manipulate the server control properties in the Page_Load method definition, as the following VB example shows:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load{
dim message as stringmessage = "Hi "ifFlash.Params.Count> 0 thenmessage = message &Flash.Params(0).ToString()& " " &Flash.Params(1).ToString()end ifFlash.Result = messageEnd Sub
In the code, if the Flash.Params.Count property is at least one, two parameters passed from Flash are appended to a string and returned to Flash.
Flash Remoting MX supports ASP.NET cookie-based state management, which maintains a user session using HTTP header information. Flash Remoting MX maintains the session ID automatically by passing the session ID to the server in each subsequent service function request. To access session variables in Flash applications using Flash Remoting MX, you can code a service function that returns session variables using the Flash.Result property.
Note: Flash Remoting MX does not support .NET cookieless state management.
Using the Flash Remoting custom control properties, you can access and set the values of session variables. Session variables persist during a browser session from page to page. If you use Flash Remoting MX to return session variables, Flash movies embedded in different pages can access a common set of data.
To enable state management in an ASP.NET application, you use the statemanagement tag in the config.web file, as the following example shows:
<configuration>
<sessionstate
mode="inproc"
cookieless="false"
timeout="20"/>
</configuration>
To return a session variable, you use the Flash.Result property, as the following example shows:
<%@ Page language="c#" debug="true" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %> <Macromedia:Flash ID="Flash" Runat="Server" /> <% Flash.Result = session.myPreference; %>
In the code, the value of the myPreference variable is assigned in the Flash.Result property, which is returned to Flash. To set a session variable using a variable passed from Flash Remoting MX, you use the Flash.Params property, as the following example shows:
<%@ Page language="c#" debug="true" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<Macromedia:Flash ID="Flash" Runat="Server" />
<%
if (Flash.Params.Count > 0)
{
session.myPreference = Flash.Params[0].ToString();
}
%>
In the code, the parameter passed from Flash is assigned into the myPreference session variable.
To return custom ASP.NET exceptions to Flash, you use the throw statement. You can throw exceptions in the context of a try/catch statement, an if/else statement, and so on. For example, the following C# snippet throws an exception:
if (Flash.Params.Count == 0)
{
throw new Exception("No arguments received.");
}
In the code, if the Flash.Params.Count variable is zero, an exception is thrown. The exception message returns to Flash as part of the onStatus error object. To display the exception in Flash, you use the error.description property, as the following ActionScript snippet shows:
function serviceFunctionName_Status (result)
{
textField = error.description;
}
In the code, the error.description property is assigned into the textField variable, which represents a text field in the Flash application. If you want to display the stack trace information returned from .NET, use the error.details property.
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRNET3.htm
Comments
No screen name said on Mar 26, 2004 at 4:40 PM : No screen name said on May 21, 2004 at 11:16 PM :