How to call asp.net webservice from flex
In this article you will learn how to call webservices hosted on asp.net applications from flex.
Create a web service on asp.net that returns the list of records. Here is how to create web service in asp.net
Asp.net web service
——————
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public List GetItems()
{
return DotGalleryController.GetItems(-1);
}
[WebMethod]
public int AddItems(string Title, string ShortDescription, string Description, string ImageUrl, string Author)
{
GalleryItemInfo item = new GalleryItemInfo();
item.Title = Title;
item.ShortDescription = ShortDescription;
item.Description = Description;
item.ImageUrl = ImageUrl;
item.EntryDate = System.DateTime.Now;
item.Author = Author;
DotGalleryController.AddItem(item);
}
}
Now its time to call webserivce from flex application.
Create mx:Webservice object and operations.
<mx:WebService id="webService" wsdl="http://localhost/dnn5com/desktopmodules/aadotgallery/WebService.asmx?WSDL"> <mx:operation name="GetItems" resultFormat="object" result="getItems_result(event);" fault="getItems_fault(event);" /> </mx:WebService>
Now create the methods to handle webservice events.
private function initApp():void
{
webService.GetItems();
}
private function getItems_result(evt:ResultEvent):void {
Alert.show("Result came!");
moviesList.dataProvider = evt.result;
}
private function getItems_fault(evt:FaultEvent):void {
Alert.show(evt.type);
}
Create a databound control to show the results.
<mx:TileList id="moviesList" rowHeight="160" columnWidth="180" columnCount="1" direction="horizontal" x="239" y="10" height="616">
<mx:itemRenderer>
<mx:Component>
<mx:VBox horizontalAlign="center" verticalAlign="middle">
<mx:Image source="{data.ImageUrl}"/>
<mx:Label text="{data.Title}" color="#000000" fontStyle="italic" />
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>






