Flex4使用HTTPService 组件用.net(C#)提供数据源,并增删改查数据库

分类:Flash_Flex_AIR| 发布:camnprbubuol| 查看: | 发表时间:2010/11/22

今天做了一个测试,用flex提供的数据提供方式HttpService,很好的实现了数据的提供,可以通过flash增删改查数据库。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="application1_applicationCompleteHandler(event)">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <s:HTTPService id="userRequest" url="http://192.168.1.110/FlexServices/Handler.ashx"
                       useProxy="false" method="POST">
            <mx:request xmlns="">
                <username>{username.text}</username> <!--username是传参的参数名,在程序里用Request.Form["username"]获得-->
                <emailaddress>{emailaddress.text}</emailaddress>
            </mx:request>        
        </s:HTTPService>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            private function send_data():void {
                userRequest.send(); //发出数据提交请求,类似与Ajax里的HttpRequest.send();
            }

            protected function application1_applicationCompleteHandler(event:FlexEvent):void
            {
                // TODO Auto-generated method stub
                userRequest.send();//默认加载数据
            }

        ]]>
    </fx:Script>
    <s:TextInput id="username" x="97" y="90"/>
    <s:TextInput id="emailaddress" x="97" y="120"/>
    <s:Button label="Submit" click="send_data()" x="249" y="120"/>
    <mx:DataGrid id="dgUserRequest" x="20" y="160"
                 dataProvider="{userRequest.lastResult.users.user}">
        <mx:columns>
            <mx:DataGridColumn headerText="User ID" dataField="userid"/>
            <mx:DataGridColumn headerText="User Name" dataField="username"/>
        </mx:columns>
    </mx:DataGrid>
    <s:TextInput x="105" y="375" id="selectedemailaddress" text="{dgUserRequest.selectedItem.emailaddress}" width="198"/>
    <s:Label x="40" y="96" text="用户名" width="49" height="22"/>
    <s:Label x="23" y="126" text="Email地址" width="59" height="22"/>
    <s:Label x="20" y="375" text="当前选择行的Email地址" width="77" height="32"/>
    <s:Label x="40" y="56" text="郑州网建" width="104" height="26" fontSize="24" fontWeight="bold"/>
</s:Application>

说明:dataProvider="{userRequest.lastResult.users.user}" 是给DataGrid控件提供数据源, lastResult是HTTPService 对象的返回结果对象,

users.user是xml的根节点.记录节点,具体试数据源提供的Xml结构而定。

 

上边是前台的。下边介绍后台处理(用了一个一般处理程序.ashx):

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;

public class Handler : IHttpHandler {

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/xml";

        #region 写入文件  //作用是:本来想用flashplayer调试版的,可以一直没找到合适的,写这段的作用是调试flash是否和处理程序通信了。
        string str = string.Empty;
        try
        {
            str = context.Request.Form["username"].Trim() + "/" + context.Request.Form["emailaddress"].Trim();
        }
        catch (Exception ex) {
            str = ex.Message.ToString();
        }
        str += " - " + DateTime.Now.ToString() + "\r\n";

        System.IO.FileStream NewFile = new System.IO.FileStream(context.Server.MapPath("/Test/log.txt"), System.IO.FileMode.Open, System.IO.FileAccess.Write);
        System.IO.StreamWriter NewStream = new System.IO.StreamWriter(NewFile);
        NewStream.WriteLine(str);
        NewStream.Flush();
        NewStream.Close();
        #endregion

//为了简化出来,动态调取数据库的方式,用下边的固定数据了。有了Request.Form["username"]获取flash提交的数据了,相信每个程序员都会接下来处理什么了。^_^

        sb.Append("<users>");
        for (int i = 0; i < 10; i++)
        {
            sb.Append("<user><userid>"+i.ToString()+"</userid><username>UserName"+i.ToString()+"</username><emailaddress>email"+i.ToString()+"@163.com</emailaddress></user>");       
        }
        sb.Append("</users>");

        context.Response.Write(sb.ToString());
        context.Response.End();
    }

    public bool IsReusable
    {
        get {
            return false;
        }
    }
}

最后运行一下:

 

本文欢迎转载,转载请注明:http://www.camnpr.com 郑州网建

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/94.html