Flex+ASP.NET+Access 留言功能(HTTPService版和URLLoader版

出处: www.RiaHome.cn 作者: www.Y-boy.cn

---------------------------------------- 主页:www.Y-boy.cn 出处:www.RiaHome.cn
本教程为 Flex+ASP.NET+Access 的留言簿

    但本例子并不像一个留言簿,请恕罪!一个留言簿,最起码有提交和显示两块。其它功能(如:修改和删除留言)道理和提交一样,也是Flex跟后台文件通讯的原理,而最终实现该功能的核心部分在后台文件。如本例子的save_mdb.aspx文件,修改一下SQL语句就能实现修改或删除留言的功能了。
    所以本教程很简单,只着重Flex跟后台文件打交道,明白这个后,其它的功能在后台文件里实现就OK了。

----------------------------------------
本教程分为:提交篇和显示篇。

    提交篇-讲述使用Flex通过ASP.NET向Access数据库写入数据,分为HTTPService版和URLLoader版。
    显示篇-讲述使用ASP.NET读取Access数据库并生成xml文件,再通过Flex显示出来。

====================================================

提交篇:HTTPService
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#f6f6f6" backgroundGradientColors="[#f6f6f6, #AA1220]" fontSize="12">
 
<!--主页:www.Y-boy.cn 出处:www.RiaHome.cn 
        我博客都不知道有什么好写了,就写写这个教程来填充一下博客.高手们别见笑了.
        注意:本例子假设你已经安装了.net框架和IIS.
        你可以使用<mx:HTTPService>标签来声明HTTPService对象.当你调用HTTPService对象的send()方法时,就会产生一个HTTP请求到你指定的url,一个HTTP响应就会返回.
        本例子是把userName变量和age变量发送到save_mdb.aspx文件,然后让save_mdb.aspx进行处理并返回处理后的信息.其中,userName变量和age变量分别对应于<mx:request>标签里的<userName>和<age>标签.也就是说把<userName>和<age>标签作为变量,把这个两标签里的内容作为变量的值.标签里使用了大括号"{}",像<userName>标签里的大括号,作用是把id为userName的<mx:TextInput>控件的text属性转换为实际的内容.因为<mx:TextInput>控件的text属性就是指向<mx:TextInput>控件的实际内容,而这些实际内容是用户输入的.
        分别说说<mx:HTTPService>标签里使用到的属性:
            id:给HTTPService控件取个名字,为了以后调用它的send()方法,必需给它取一个名字,(如下面的<mx:Button>控件 click="my_HS.send()");
           
            url:后台文件的路径.本例子里你必需更改这个url值,改为save_mdb.aspx文件在你电脑里的实际路径.这个save_mdb.aspx文件在压缩包里的dataBase文件夹里.注意,我这里是用"http://localhost"开头的路径,而不是类似"E:\asp\flexguestbook"的路径,这是让save_mdb.aspx文件在.net环境下正常工作,前提是你已经装上.net框架和IIS.
              
            method:这个是指定以何种方式发送HTTP请求.这里使用了GET方法.
           
            resultFormat:指定以何种格式反回结果.这里要返回字符串,所以使用了"text".默认情况下,resultFormat返回的是对象.
           
            result:当处理完发送的请求,并返回结果时,就执行这个属性.这里的操作是:把返回的内容赋值给id为resultInfo的<mx:TextArea>控件的text属性,也就是让返回的内容在<mx:TextArea>控件里显示.其中的"event.result"就是返回的内容,这里用String()函数把返回的内容强制转换为字符串类型.因为<mx:TextArea>控件的text属性类型是字符串类型,这样转换使两者的类型匹配.
                  
    
-->
    
<mx:HTTPService id="my_HS" url="http://localhost/E/ASP.NET_Lab/save_mdb.aspx" method="GET" resultFormat="text" result="{resultInfo.text = String(event.result)}">
       
<mx:request>
           
<userName>{userName.text}</userName>
           
<age>{age.text}</age>
       
</mx:request>
    
</mx:HTTPService>
    
    
    
<mx:Label x="10" y="10" text="Flex+ASP.NET+Access = GuestBook (使用HTTPService)" fontSize="20" fontWeight="bold"/>
    
<mx:LinkButton label="作者:www.Y-boy.cn" click="navigateToURL(new URLRequest('http://www.Y-boy.cn'),'_blank')" x="804" y="27"/>
    
<mx:LinkButton label="出处:www.RiaHome.cn" click="navigateToURL(new URLRequest('http://www.RiaHome.cn'),'_blank')" x="637" y="27"/>
    
<mx:HRule x="10" y="49" width="98%"/>
    
    
    
    
<mx:Label text="用户名:" x="23" y="61"/>
    
<mx:TextInput id="userName" x="77" y="59"/>
    
    
<mx:Label text="年龄:" x="35" y="91"/>
    
<mx:TextInput id="age" x="77" y="89"/>
    
    
<mx:Button label="提交" click="my_HS.send()" x="185" y="121"/>
    
    
<mx:Label text="反馈信息:" x="10" y="159"/>
    
<mx:TextArea id="resultInfo" x="10" y="179" width="234" height="188"/>
 
</mx:Application>
 
提交篇:URLLoader版
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#f6f6f6" backgroundGradientColors="[#f6f6f6, #AA1220]" fontSize="12">
    
<!--主页:www.Y-boy.cn 出处:www.RiaHome.cn 
        我博客都不知道有什么好写了,就写写这个教程来填充一下博客.高手们别见笑了.
        注意:本例子假设你已经安装了.net框架和IIS.
        这个AS3.0版的Flex留言簿就不多说了,详细可以看AS3.0的中文帮助.Flash CS3 使用、AS3帮助教程、Flash CS3组件帮助、Flash Video Encoder 用户手册 PDF下载:http://blog.5d.cn/user39/riahome/200707/417090.html
    PS:在Flex建议不采用AS3.0这种形式,取而代之使用<mx:HTTPService>标签.
       
    注意:那个save_mdb.aspx文件在压缩包里的dataBase文件夹里.测试前必需把下面的"http://localhost/E/ASP.NET_Lab/save_mdb.aspx"路径改为save_mdb.aspx文件在你电脑里的实际路径.我这里是用"http://localhost"开头的路径,而不是类似"E:\asp\flexguestbook"的路径,这是让save_mdb.aspx文件在.net环境下正常工作,前提是你已经装上.net框架和IIS.
    
-->
    
<mx:Script>
       
<![CDATA[
       
           import flash.net.URLLoader;
           import flash.net.URLRequest;
           import flash.net.URLLoaderDataFormat;
           import flash.net.URLVariables;
           import flash.events.Event;
           import flash.events.HTTPStatusEvent;
           import flash.events.IOErrorEvent;
           import flash.events.ProgressEvent;
           import flash.events.SecurityErrorEvent;
           
           private var variables:URLVariables=new URLVariables();
           private var request:URLRequest=new URLRequest("http://localhost/E/ASP.NET_Lab/save_mdb.aspx");
           private var loader:URLLoader=new URLLoader();
           
           private function sendInfo(e:Event):void{
              variables.userName=userName.text;
              variables.age=age.text;
              
              request.data = variables;
              request.method = URLRequestMethod.GET;
              
              loader.dataFormat = URLLoaderDataFormat.TEXT;
              loader.addEventListener(Event.COMPLETE, loader_complete);
              loader.addEventListener(Event.OPEN, loader_open);
              loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loader_httpStatus);
              loader.addEventListener(ProgressEvent.PROGRESS, loader_progress);
              loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_security);
              loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioError);
              loader.load(request);
           }
           private function loader_complete(e:Event):void{
              resultInfo.text="Event.COMPLETE";
           }
           private function loader_open(e:Event):void{
              resultInfo.text="Event.OPEN";
           }
           private function loader_httpStatus(e:HTTPStatusEvent):void{
              resultInfo.text="HTTPStatusEvent.HTTP_STATUS";
           }
           private function loader_progress(e:ProgressEvent):void{
              resultInfo.text="ProgressEvent.PROGRESS";
           }
           private function loader_security(e:SecurityErrorEvent):void{
              resultInfo.text="SecurityErrorEvent.SECURITY_ERROR";
           }
           private function loader_ioError(e:IOErrorEvent):void{
              resultInfo.text="IOErrorEvent.IO_ERROR";
           }
       
]]>
    
</mx:Script>
    
    
<mx:Label x="10" y="10" text="Flex+ASP.NET+Access = GuestBook (使用URLLoader)" fontSize="20" fontWeight="bold"/>
    
<mx:LinkButton label="作者:www.Y-boy.cn" click="navigateToURL(new URLRequest('http://www.Y-boy.cn'),'_blank')" x="788" y="27"/>
    
<mx:LinkButton label="出处:www.RiaHome.cn" click="navigateToURL(new URLRequest('http://www.RiaHome.cn'),'_blank')" x="621" y="27"/>
    
<mx:HRule x="10" y="49" width="98%"/>
    
    
    
<mx:Label text="用户名:" x="23" y="61"/>
    
<mx:TextInput id="userName" x="77" y="59"/>
    
    
<mx:Label text="年龄:" x="35" y="91"/>
    
<mx:TextInput id="age" x="77" y="89"/>
    
    
<mx:Button label="提交" click="sendInfo(event)" x="185" y="121"/>
    
    
<mx:Label text="反馈信息:" x="10" y="159"/>
    
<mx:TextArea id="resultInfo" x="10" y="179" width="234" height="188"/>
</mx:Application>
原文地址:https://www.cnblogs.com/schyu/p/1295800.html