Ever wonder how to send data back and forth between ASP.NET and Flash?
About a year ago, we designed and built the DIY Floor Plan website (
http://www.diyfloorplan.com). We needed the ability to send data between the Flash Floor Plan Designer and the ASP.NET web pages that saved the data to the database. We wanted the floor plan designer to be as "dumb" as possible...simply design the floor plan and allow the ASP.NET application manage the data. Here's some quick code snippets that will show you how to do it very easily.
ViewFloorPlan.aspxThis page displays the public, non-editable version of the floor plan. The important thing here is that we put the Floor Plan GUID in a Flash Parameter. We use a GUID to prevent users from seeing floor plans that are not publicly available by the floor plan administrator. The Flash application uses the GUID as the key to communicate with the ASP.NET application.
1<script type="text/javascript">
2 var so = new SWFObject("/flash/map_client.swf", "map_client", "775", "650", "6", "#ececec");
3 so.addParam("menu", "false");
4 so.addParam("quality", "high");
5 so.addParam("wmode", "transparent");
6 so.addParam("allowScriptAccess", "sameDomain");
7 so.addParam("movie", "/flash/map_client.swf");
8 so.addParam("bgcolor", "#ffffff");
9 so.addParam("FlashVars", "FloorPlanGUID=<% GetFloorPlanGUID(); %>");
10 so.write("flashcontent1");
11</script>FlashGetData.aspxThis page is used by the Flash application to get data. The ASPX portion of this web page has no HTML or controls. When processing the Flash application's request, we output the data it needs in the HTML in a name-value pair. A little explanation...this page is called for the floor plan XML or an individual booth's details (name, address, etc.). After calling the database to get the data, it's written to the output HTML as
field=value.
1public partial class FlashGetData : System.Web.UI.Page
2{
3protected void Page_Load(object sender, EventArgs e)
4 {
5 if (Request.QueryString["Mode"] != null)
6 {
7 try
8 {
9 //Extract mode
10 Enums.ModeEnum mode =
(Enums.ModeEnum)int.Parse(Request.QueryString["Mode"].ToString());
11
12 //Process based on mode
13 DataSet ds = null;
14 DataRow row;
15 if (mode == Enums.ModeEnum.RequestFloorPlan)
16 {
17 //Extract Querystring parameters
18 string floorPlanGUID = Request.QueryString["FloorPlanGUID"].ToString();
19
20 //Get the data
21 ds = FloorPlans_BLL.SelectFloorPlansByFloorPlanGUID(new Guid(floorPlanGUID));
22 row = ds.Tables[0].Rows[0];
23 Response.Write("FloorPlanXML=" + row["FloorPlanXML"].ToString());
24 }
25 else if (mode == Enums.ModeEnum.RequestCompanyDetails)
26 {
27 //Extract Querystring parameters
28 string floorPlanGUID = Request.QueryString["FloorPlanGUID"].ToString();
29 string boothID = Request.QueryString["BoothID"].ToString();
30
31 //Get the data
32 ds = Companies_BLL.SelectCompaniesByFloorPlanIDBoothID(
new Guid(floorPlanGUID), boothID);
33 Response.Write("CompanyDetails=" + BuildCompanyDetails(boothID, ds));
34 }
35 }
36 catch (Exception ex)
37 {
38 Helpers.ProcessException(ex);
39 }
40 }
41 }
42}FlashSaveData.aspx
This page is used in the administration area. Again, the ASPX portion of this web page has no HTML or controls.
The Flash movie executes a command like: xmlDoc.sendAndLoad("FlashSaveData.aspx?FloorPlanGUID=xxx-xxx-xxx-xxx", returnXML, "POST");
The important point of this code is Line 19. The Flash application posts data in the Request stream.
1public partial class FlashSaveData : System.Web.UI.Page
2{
3protected void Page_Load(object sender, EventArgs e)
4 {
5 //The Flash movie executes a command like:
6 //xmlDoc.sendAndLoad("FlashSaveData.aspx?FloorPlanGUID=xxx-xxx-xxx-xxx",
returnXML, "POST");
7
8 XmlDocument doc = null;
9 FloorPlans floorplans = null;
10 FloorPlanCompanies fpc = null;
11 DataSet ds = null;
12 try
13 {
14 if (Request.QueryString["FloorPlanGUID"] != null)
15 {
16 //Extract GUID and XML
17 string floorPlanGUID = Request.QueryString["FloorPlanGUID"].ToString();
18 doc = new XmlDocument();
19 doc.Load(Request.InputStream);
20
21 //Get the FloorPlan row
22 ds = FloorPlans_BLL.SelectFloorPlansByFloorPlanGUID(new Guid(floorPlanGUID));
23 DataRow row = ds.Tables[0].Rows[0];
24
25 //Build FloorPlans object
26 floorplans = new FloorPlans();
27 floorplans.FloorPlanID = Int32.Parse(row["FloorPlanID"].ToString());
28 floorplans.FloorPlanGUID = new Guid(row["FloorPlanGUID"].ToString());
29 floorplans.UserID = Int32.Parse(row["UserID"].ToString());
30 floorplans.Title = row["Title"].ToString();
31 floorplans.URL = row["URL"].ToString();
32 floorplans.FloorPlanXML = doc.InnerXml;
33 floorplans.Active = (bool)row["Active"];
34 floorplans.RewriterID = Int32.Parse(row["RewriterID"].ToString());
35
36 //Make sure all booths are now in the database
37 XmlNodeList objNodes = doc.SelectNodes("./floorplan/booths/boothtxt");
38 foreach (XmlNode objNode in objNodes)
39 {
40 fpc = new FloorPlanCompanies();
41 fpc.FloorPlanID = Int32.Parse(row["FloorPlanID"].ToString());
42 fpc.BoothID = objNode.InnerXml;
43 fpc.CompanyID = 0;
44 FloorPlanCompanies_BLL.SaveFloorPlanCompanies(fpc, false, null);
45 }
46
47 //Save to database
48 FloorPlans_BLL.UpdateFloorPlans(floorplans);
49 }
50 else
51 throw new Exception("FloorPlanGUID missing");
52 }
53 catch (Exception ex)
54 {
55 Helpers.ProcessException(ex);
56 }
57 finally
58 {
59 if (ds != null)
60 {
61 ds.Dispose();
62 ds = null;
63 }
64 fpc = null;
65 floorplans = null;
66 doc = null;
67 }
68 }
69}
Comments
|
On
9/19/2009
Ethan
said:
Thanks!
On
2/9/2009
Kevin
said:
Thanks for the comprehensive details. Will look into trying this out as soon as I can digest it all :)
|
Leave a Comment