Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] - نسخه متنی

Andy Wigley; Peter Roxburgh

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید






previous section might have led you to deduce that you can pass ADO.NET DataSet objects to XML Web services, which can also return DataSet objects. The DataSet object is transferred between an application and an XML Web service. The ability to transfer disconnected DataSet objects like this allows you to manipulate data in ways that aren't possible in applications where a persistent connection to a database is required. For example, you now can perform the following tasks:



Receive a large quantity of data in a single DataSet object and format the data to best suit the display characteristics of mobile devices



Perform queries within an application on a DataSet that an XML Web service returns and then display only the application-level query results to the client



Update a DataSet with a user's input and pass this to an XML Web service, which can then update the remote data store



Pass DataSet objects obtained from a local source to an XML Web service for remote processing



DataSet objects passed to or returned from XML Web services provide an incredibly flexible approach to designing and writing data-driven mobile Web applications. This flexibility is best illustrated through an example. Chapter 11.

Listing 15-5: DataAccessWebService XML Web service, which returns a DataSet object






using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
namespace MSPress.MobWeb.DataAccessWebService
{
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public DataSet GetHeights()
{
//Change the path to the database if it isn't installed at
//the location defined here.
string strMyConnection =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
"'C:\\Inetpub\\wwwroot\\DataAccessWebService\\Mountain.mdb'";
string strMySelect = "SELECT * FROM Mountains";
// Create a new connection.
OleDbConnection connection =
new OleDbConnection(strMyConnection);
// Create the DataSet.
DataSet myDataSet = new DataSet();
// Create a new adapter.
OleDbDataAdapter mycommand =
new OleDbDataAdapter(strMySelect,connection);
// Fill the DataSet.
mycommand.Fill(myDataSet,"Mountains");
return myDataSet;
}
}
}











You'll now write a mobile Web Forms page that consumes the XML Web service. The page in Listing 15-6 initially displays a list of choices to the user as well as a Command control. The code-behind module shown in Listing 15-7 accesses the XML Web service, which then returns a DataSet. Next you write a switch statement, which tests the SelectedIndex value of the SelectionList. Based on the user's input, the switch statement sets the string myQuery to a filter statement that you apply to a DataView object that you build from the DataSet. You can't apply filters directly to a DataSet, so you must first create a DataView object, as we do here.

Listing 15-6: MobileWebForm1.aspx of the ConsumeDataAccessWebService sample application






<%@ Register TagPrefix="mobile" 
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<%@ Page language="c#"
Codebehind="MobileWebForm1.aspx.cs"
Inherits="MSPress.MobWeb.ConsumeDataAccessWebService.MobileWebForm1" %>
<mobile:form id="Form1" runat="server">
<mobile:SelectionList id="SelectionList1" runat="server">
<Item Text="All mountains"></Item>
<Item Text="Above 1000m"></Item>
<Item Text="Below 1000m"></Item>
</mobile:SelectionList>
<mobile:Command id="Command1" runat="server">View</ mobile:Command>
</mobile:form>
<mobile:form id="Form2" runat="server">
<mobile:List id="List1" runat="server"></mobile:List>
</mobile:form>












Listing 15-7: Code-behind module MobileWebForm1.aspx.cs of the ConsumeDataAccessWebService sample






using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Mobile;
using System.Web.UI;
using System.Web.UI.MobileControls;
using ConsumeDataAccessWebService.localhost;
namespace MSPress.MobWeb.ConsumeDataAccessWebService
{
public class MobileWebForm1 : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.SelectionList SelectionList1;
protected System.Web.UI.MobileControls.Command Command1;
protected System.Web.UI.MobileControls.Form Form2;
protected System.Web.UI.MobileControls.List List1;
protected System.Web.UI.MobileControls.Form Form1;
public MobileWebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Command1.Click += new
System.EventHandler(this.Command1_Click);
}
private void Command1_Click(object sender, System.EventArgs e)
{
String myQuery=";
// Create a new instance of the XML Web service.
Service1 myService = new Service1();
// Get a DataSet from the XML Web service.
DataSet myDataSet = myService.GetHeights();
// Build a query.
switch (SelectionList1.SelectedIndex)
{
case 0: myQuery=";
break;
case 1: myQuery="HeightMeters > 1000";
break;
case 2: myQuery="HeightMeters < 1000";
break;
}
// Create a new DataView using the Mountains table.
DataView dv= new DataView(myDataSet.Tables["Mountains"]);
// Run the filter to get the desired results.
dv.RowFilter=myQuery;
// Set the datasource of the list to the DataView.
List1.DataSource=dv;
// The name of the field to display
List1.DataTextField="Mountain";
// Bind
List1.DataBind();
// Set the active form.
this.ActiveForm=Form2;
}
}
}











Figure 15-8 shows the output that appears after this code executes.


Figure 15-8: Output of a mobile Web application that consumes an XML Web service that passes data as DataSet objects

/ 145