Flex 通过Amf 与ASP.net 的交互方法注意事项

AMF 是一种比较方便的 Flex 与 ASP.Net  交互的方式

前一段时间发现自己还是有一些技术细节没有掌握,浪费了很多时间,在这里记录一下,以观后效

1.后台传递DataTable集合

ASP.Net 后台部分

代码
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using Hp.model;
using Hp.data;
using Hp.data.Tool;
using FluorineFx;
using FluorineFx.AMF3;
using Spring.Context;
using Spring.Context.Support;
using System.Configuration;
using System.IO;
using Newtonsoft.Json;
 
[RemotingService]//注意这里要加上
public class SurveyService
{
}


[DataTableType(
"Flex")]//注意这里要加上
public DataTable getStat(int qid)
{
    
string strSql = "select label,dbo.fGetEventAnswerCount(questionid,[ID]) as data from STB_Options where  questionid = " + qid;
     DataSet ds 
= SqlHelper.GetDatasetSql(strSql);

            
if (ds != null)
            {
                
return (DataTable)ds.Tables[0];
            }
            
else
            {
                
return null;
            }
}

 

 

Flex 部分接受到的数据是这样的

 

具体方法代码如下:

 

 

代码
    <mx:Script>
        
<![CDATA[
            
import mx.collections.ArrayCollection;
            
import mx.rpc.events.FaultEvent;
            
import mx.rpc.events.ResultEvent;
            
import mx.controls.Alert;
            
import mx.events.EffectEvent;
            
            [Bindable]
            
private var AnswerList:ArrayCollection = new ArrayCollection();
            
            
private function init():void{
                remote.getStat(
5);
                ExternalInterface.addCallback(
"ChangeQuestion",ChangeQuestion);
            }
            
            
public function ChangeQuestion(a : int) : void{
                remote.getStat(a);
            }
            
            
private function handleResult(event:ResultEvent):void{
                AnswerList 
= event.result as ArrayCollection;//这里注意
            }
            
            
private function handleFault(event:FaultEvent):void{
                Alert.show(event.fault.message.toString());            
            }
        ]]
>


<mx:RemoteObject id="remote" destination="fluorine" source="Hp.data.Services.SurveyService" >//这里注意服务名称 fault="handleFault(event)">
    <mx:method name="getStat" //这里注意方法名称 result="handleResult(event)"/>
</mx:RemoteObject>

 

 

另外 Flex 项目配置 AMF 也要注意

创建项目时选择 ASP.NET 后台 如图:

 

注意服务器配置,如下

 

配置Config 文件

 

 

最后检查一下服务

 

当然还要引用AMF的包,自己找吧. 

 

 

 

 

 

posted on 2011-02-14 11:56  Cheney Hao  阅读(402)  评论(0编辑  收藏  举报

导航