flex+asp.net+webservice+String+DataT

本文以.NET平台下C#语言开发的WebService作为远程数据源,详细介绍Flex与.NET的WebService的数据通信知识点;包括连接WebService,远程调用WebService方法,给WebService方法传递参数等相关知识点。三个标签的使用方法基本上是一样,这里就以标签为例进行介绍。
首先看看如下代码块:
Books.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HelloWorldWebService
{
public class Book
{
public int Id { get; set; }
public string Name{get;set;}
public string Author{get;set;}
public int Price { get; set; }
}
}
Service1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Services;
namespace HelloWorldWebService
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string str)
{
return str+ "Hello World";
}
[WebMethod]
public Book GetBook()
{
return new Book
{
Id = 1,
Name = "三国演义",
Author = "罗贯中",
Price = 100
};
}
[WebMethod]
public DataTable GetDataTable()
{
DataTable dt = new DataTable("Books");
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Author", typeof(string));
dt.Columns.Add("Price", typeof(double));
DataRow dr = dt.NewRow();
dr["Id"] = 1;
dr["Name"] = "<flex游戏开发>";
dr["Author"] = "张三";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Id"] = 2;
dr["Name"] = "《Flash游戏开发》";
dr["Author"] = "李四";
dr["Price"] = 65.50;
dt.Rows.Add(dr);
return dt;
}
[WebMethod]
public List<Book> BookList()
{
return new List<Book>
{
new Book
{
Id = 1,
Name = "<chenshaoyong>",
Author = "tiankongfeiyang",
Price = 12
},
new Book
{
Id = 2,
Name = "llll",
Author = "lisi",
Price = 11
}
};
}
}
}
flex
<?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/halo" minWidth="1024" minHeight="768" xmlns:helloworld="services.helloworld.*" xmlns:getbook="services.getbook.*" xmlns:gettabledata="services.gettabledata.*" xmlns:getlist="services.getlist.*">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.controls.Alert;
protected function button_clickHandler(event:MouseEvent):void
{
HelloWorldResult.token = helloWorld.HelloWorld(this.text.text);
}
protected function dataGrid_creationCompleteHandler(event:FlexEvent):void
{
GetBookResult.token = getBook.GetBook();
}
protected function button2_clickHandler(event:MouseEvent):void
{
GetDataTableResult.token = getTableData.GetDataTable();
}
protected function dataGrid3_creationCompleteHandler(event:FlexEvent):void
{
BookListResult.token = getlist.BookList();
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="HelloWorldResult"/>
<helloworld:HelloWorld id="helloWorld" fault="Alert.show(event.fault.faultString)" showBusyCursor="true"/>
<s:CallResponder id="GetBookResult"/>
<getbook:GetBook id="getBook" fault="Alert.show(event.fault.faultString)" showBusyCursor="true"/>
<s:CallResponder id="GetDataTableResult"/>
<gettabledata:GetTableData id="getTableData" fault="Alert.show(event.fault.faultString)" showBusyCursor="true"/>
<s:CallResponder id="BookListResult"/>
<getlist:Getlist id="getlist" fault="Alert.show(event.fault.faultString)" showBusyCursor="true"/>
</fx:Declarations>
<s:TextInput id="text" x="71" y="42"/>
<s:Button x="276" y="43" label="按钮" id="button" click="button_clickHandler(event)"/>
<mx:Label x="71" y="76" text="{HelloWorldResult.lastResult}" id="label"/>
<mx:DataGrid x="74" y="116" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{GetBookResult.lastResult}" editable="true">
<mx:columns>
<mx:DataGridColumn headerText="编号" dataField="Id"/>
<mx:DataGridColumn headerText="书名" dataField="Name"/>
<mx:DataGridColumn headerText="作者" dataField="Author"/>
<mx:DataGridColumn headerText="价格" dataField="Price"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid x="484" y="116" id="dataGrid2" dataProvider="{GetDataTableResult.lastResult.Tables.Books.Rows}" editable="true">
<mx:columns>
<mx:DataGridColumn headerText="编号" dataField="Id"/>
<mx:DataGridColumn headerText="书名" dataField="Name"/>
<mx:DataGridColumn headerText="作者" dataField="Author"/>
<mx:DataGridColumn headerText="价格" dataField="Price"/>
</mx:columns>
</mx:DataGrid>
<s:Button x="813" y="269" label="按钮" id="button2" click="button2_clickHandler(event)"/>
<mx:DataGrid x="76" y="341" id="dataGrid3" creationComplete="dataGrid3_creationCompleteHandler(event)" dataProvider="{BookListResult.lastResult}" editable="true">
<mx:columns>
<mx:DataGridColumn headerText="Id" dataField="Id"/>
<mx:DataGridColumn headerText="Name" dataField="Name"/>
<mx:DataGridColumn headerText="Author" dataField="Author"/>
<mx:DataGridColumn headerText="Price" dataField="Price"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
Tags: 

延伸阅读

最新评论

发表评论