NET Mobile Tutorial

15
.NET Mobile Tutorial Cell phones (mobile phones) have become a part of our life style. This tutorial is about how to develop mobile applications with an extension to the .NET Framework, called the Microsoft Mobile Internet Toolkit (MMIT) or simply .NET Mobile. .NET Mobile Introduction Background Cell phones (mobile phones) have become part of our life style. Today, mobile devices can connect to the Internet, and execute web applications. Mobile applications can now be developed to deliver any types of data, to any user, any place in the world! Different mobile devices support different programming languages. Some support WAP and WML, some support HTML or a limited version of HTML, and some support both or a different language. To support all types of mobile devices, developers must create one different application for each language. With .NET Mobile, Microsoft has introduced a new platform for developing mobile applications. This tutorial is about how to develop applications with an extension to the .NET Framework, called the Microsoft Mobile Internet Toolkit (MMIT) or simply .NET Mobile. What You Should Already Know Before you continue you should have a basic understanding of the following: HTML / XHTML XML namespaces ASP .NET .NET Mobile .NET Mobile is an extension to Microsoft ASP.NET and the Microsoft's .NET Framework. .NET Mobile is a set of server-side Forms Controls to build applications for wireless mobile devices. These controls produce different output for different devices by generating WML, HTML, or compact HTML. How Does It Work? The following table shows how the .NET Mobile works: Mobile Devices The Internet Internet Information Services - IIS The .NET Framework ASP.NET .NET Mobile 1

Transcript of NET Mobile Tutorial

Page 1: NET Mobile Tutorial

.NET Mobile Tutorial

Cell phones (mobile phones) have become a part of our life style.This tutorial is about how to develop mobile applications with an extension to the .NET Framework, called the Microsoft Mobile Internet Toolkit (MMIT) or simply .NET Mobile.

.NET Mobile Introduction BackgroundCell phones (mobile phones) have become part of our life style.Today, mobile devices can connect to the Internet, and execute web applications.Mobile applications can now be developed to deliver any types of data, to any user, any place in the world!Different mobile devices support different programming languages. Some support WAP and WML, some support HTML or a limited version of HTML, and some support both or a different language.To support all types of mobile devices, developers must create one different application for each language.With .NET Mobile, Microsoft has introduced a new platform for developing mobile applications.This tutorial is about how to develop applications with an extension to the .NET Framework, called the Microsoft Mobile Internet Toolkit (MMIT) or simply .NET Mobile. What You Should Already Know

Before you continue you should have a basic understanding of the following: HTML / XHTML XML namespaces ASP .NET

.NET Mobile

.NET Mobile is an extension to Microsoft ASP.NET and the Microsoft's .NET Framework.

.NET Mobile is a set of server-side Forms Controls to build applications for wireless mobile devices.These controls produce different output for different devices by generating WML, HTML, or compact HTML.

How Does It Work?The following table shows how the .NET Mobile works:

Mobile DevicesThe Internet

Internet Information Services - IISThe .NET Framework

ASP.NET.NET Mobile

A web client requests a web page The request travels the Internet The request is received by IIS The request is handled by the .NET framework The requested page is compiled by ASP.NET .NET Mobile handles any mobile device requirements The page is returned to the client

Software RequirementsTo develop mobile applications with .NET Mobile, you must have the following components:

1. Windows 2000 Professional/Server with IIS 52. All Windows 2000 service packs3. The ASP.NET framework4. Microsoft Mobile Internet Toolkit (MMIT)5. Internet Explorer 5.5 or later6. A WAP simulator

1

Page 2: NET Mobile Tutorial

You will need Windows 2000 to develop .NET applications (IIS 5 (Internet Information Services) is a part of Windows 2000). You also have to install MMIT (.NET Mobile).Internet Explorer and MMIT can be downloaded from Microsoft MSDN.

How To Start1. Developing a mobile web application with ASP.NET is very easy:2. Create an ASP.NET page3. Include System.Mobile.UI 4. Add Mobile Controls to the page

.NET Mobile Example The Mobile ASP.NET PageMobile Controls are the main building blocks of mobile applications.Mobile Controls are similar to Web Controls in ASP.NET.The following ASP.NET page displays "Hello W3Schools" as a WML card in a WML enabled cell phone:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage" %><%@ RegisterTagPrefix="mob"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><mob:Form runat="server"> <mob:Label runat="server">Hello W3Schools</mob:Label></mob:Form>

The Page directive tells ASP to use (inherit) mobile page handling instead of regular page handling (like the one used for traditional browsers).The Register directive defines the prefix that will be used for mobile controls. We have used "mob", but you can use any prefix you like. The <mob:Form> element tells the server to create a mobile form control. The <mob:Label> element tells the server to create a mobile label control with the text "Hello W3Schools".

When the ASP .NET page executes, it will produce the following output to a WAP enabled mobile phone: <?xml version='1.0'?><!DOCTYPE wml PUBLIC'-//WAPFORUM//DTD WML 1.1//EN''http://www.wapforum.org/DTD/wml_1.1.xml'><wml><card><p>Hello W3Schools</p></card></wml>

and this output will be produced for a Pocket PC:<html><body><form id="ctrl1" name="ctrl1" method="post"action="example.aspx"><div>Hello W3Schools</div></form></body></html>

Conclusion.NET Mobile will generate WML code for WAP enabled cell phones and HTML code for devices like the Pocket PC.

2

Page 3: NET Mobile Tutorial

By detecting the browser, .NET Mobile will output correct content, providing developers with a powerful tool to develop single applications that will serve many different mobile devices.

.NET Mobile Emulators Mobile applications can be tested and viewed with different emulators.

Using your BrowserSince mobile web pages detect your browser, you can test your mobile applications with a standard browser.When a mobile web page detects a web browser it will produce HTML output.

Openwave SimulatorsThe Openwave mobile browser is the most common browser used in Internet-enabled cellphones.An emulator can be downloaded from http://www.openwave.com

The Nokia Mobile Internet ToolkitThis toolkit contains emulators for most of the different Nokia devices.This toolkit can be downloaded from http://forum.nokia.com

Windows Mobile Development PlatformHere you'll find answers to basic questions regarding the Windows Mobile development platform, along with different downloads:http://msdn.microsoft.com/en-us/windowsmobile/bb250560.aspx

.NET Mobile Forms .NET Mobile Forms are specialized web forms designed to work on different mobile devices.

Mobile PagesA mobile page is much the same as an ordinary .NET web page. It is a text file with an aspx extension, and it can contain a variety of web controls.The difference is the page directive that identifies the page as a mobile page, and the controls used on the page, which are mobile controls.The mobile controls can be programmed device-independently, and the page will produce an output that suits the device that access it.

Mobile FormsEach mobile page must have at least one mobile form, and each mobile form can have a number of mobile controls.Note that mobile pages can have multiple mobile forms. This is due to the nature of mobile devices. Mobile devices have small screens and it is very normal to navigate between screens with a simple link.

Automatic Paging.NET Mobile supports automatic paging for different mobile devices.The paging is handled differently for each control. For example when paging takes place the controls included in a panel control will stay together.

Displaying TextThis mobile page uses a TextView control to display a large amount of text:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %>

3

Page 4: NET Mobile Tutorial

<Mobile:Form runat="server"><Mobile:TextView runat="server">This is a very long text to demonstratehow text can be displayed over several screens.This is a very long text to demonstratehow text can be displayed over several screens.This is a very long text to demonstratehow text can be displayed over several screens.This is a very long text to demonstratehow text can be displayed over several screens.</Mobile:TextView></Mobile:Form>

When this page is displayed on a mobile device, the navigation and display functions of the page will be compiled differently for different devices with different display characteristics.When the text is displayed on a pocket PC with a small display, the user will be able to scroll the text with a scroll bar, but on a cell phone the text will be displayed over several screens with proper navigation tools added.Note that all mobile controls must have the runat attribute set to "server", in order to secure proper rendering of the page for different devices.

Single FormsThis mobile page has one form:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %>

<Mobile:Form runat="server"> <Mobile:Label runat="server">Hello W3Schools </Mobile:Label></Mobile:Form>

Multiple FormsThis mobile page has two forms:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><Mobile:Form id="f1" runat="server"> <Mobile:Label runat="server">Hello W3Schools </Mobile:Label> <Mobile:Link runat="server" NavigateURL="#f2">2 </Mobile:Link></Mobile:Form><Mobile:Form id="f2" runat="server"> <Mobile:Label runat="server">Hello Again </Mobile:Label> <Mobile:Link runat="server" NavigateURL="#f1">1 </Mobile:Link></Mobile:Form>

LinksNote the <Mobile:Link> element in the example above. The link control lets the user navigate between the two mobile forms.

4

Page 5: NET Mobile Tutorial

.NET Mobile Events

Mobile Controls exposes device independent programmable events.

Programming EventsMobile controls have an object model with programmable properties, methods and events.For a complete overview please refer to the reference section.

Submitting TextThis page has two forms:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><script runat="server">Dim ageSub AgeClick(sender As Object, e As EventArgs)age=text1.TextActiveForm=Form2End SubSub Form2_Activate(sender As Object,e As EventArgs) message.Text="You are " & age & " years old"End Sub</script><Mobile:Form id="form1" runat="server"><Mobile:Label runat="server">Age?</Mobile:Label><Mobile:TextBox runat="server" id="text1" /><Mobile:Command runat="server" OnClick="AgeClick" Text="Submit" /></Mobile:Form><Mobile:Form id="form2" runat="server" OnActivate="Form2_Activate"><Mobile:Label runat="server" id="message" /></Mobile:Form>

When a page has two forms, the first form is always opened by default.The first form has a label with the text "Age?", an input box to input the age, and a submit button.The second page is activated by the submit button on the first page, and displays a response.

.NET Mobile Input Input Controls are used to collect input from the mobile user.

Input ControlsThere is a number of mobile controls that support user input.The most common input control is perhaps the TextBox control, which was demonstrated in the previous chapter. The TextBox control is perfect for simple user input like names, numbers, identifications and keywords.For larger amount of input a TextView control is a better choice. The TextView control allows long multi-line input like the one you need for SMS or other messages.

Numeric InputThe numeric attribute of the textbox control can be set to true or false to specify whether the textbox should accept only numeric input.Note: This behavior will normally work on cell phones by changing the input mode from letters to numbers. For HTML browsers however, this behavior is normally not supported.

5

Page 6: NET Mobile Tutorial

Password InputThe password attribute of the textbox control can be set to true or false to specify whether the textbox should be treated as a password field.A password field will hide the input by displaying * (stars) instead of ordinary text.

List ControlsTextBox and TextView controls are well suited for entering input, but sometimes you want the user to select from a list of predefined values.The SelectionList Control supports drop down lists, check boxes and radio buttons. This topic is covered in another chapter.The List Control supports selection from menus and lists. The List Control is covered in another chapter.

User Interface ControlsUser Interface Controls are controls which display the user interface:

Name FunctionCommand Performs an actionForm Defines a container for mobile controlsImage Defines an imageLabel Defines a textLink Defines a hyperlinkList Defines a listMobilePage Defines a container for mobile controlsObjectList Defines a list of data objectsPanel Defines a container for other controlsSelectionList Defines a list to select fromStyleSheet Defines styles to be applied to other controlsTextBox Defines a single line input boxTextView Defines a multi-line input boxFor a full control reference, including properties methods, events, and more examples, please look at the "Mobile Reference" page.

.NET Mobile Input Validation Validation Controls are used to validate the data entered by a user.

Validation ControlsValidation controls are used to validate the data entered by a user.Validation controls allow you to validate an input control (like a TextBox), and display a message when validation fails.Each validation control performs a specific type of validation (like validating against a specific value or a range of values).By default, page validation is performed when a command control is clicked. You can prevent validation when a control is clicked by setting the CausesValidation property to false.

Validating Input

This page has two forms:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><script runat="server">Sub Page2(Sender as Object,E as EventArgs)If Page.IsValid Then ActiveForm=f2

6

Page 7: NET Mobile Tutorial

text2.Text="You are " & age.text & " years old"end ifEnd Sub</script><Mobile:Form id="f1" runat="server"><Mobile:CompareValidator runat="server" ControlToValidate="age" Type="Integer" ValueToCompare="18" Operator="GreaterThanEqual">You must be at least 18</Mobile:CompareValidator><Mobile:Label runat="server">Age?</Mobile:Label><Mobile:TextBox id="age" runat="server" /><Mobile:Command OnClick="Page2" runat="server">Submit</Mobile:Command></Mobile:Form><Mobile:Form id="f2" runat="server"><Mobile:Label id="text2" runat="server" /></Mobile:Form>

The first form has a label with the text "Age?", an input box to input the age, and a submit button.The second page is activated by the submit button on the first page, and displays a response.If the input validates as error, an error message is displayed.

The ValidationSummary ControlThe previous example used a CompareValidator control to validate an input field. The field to validate was defined by the attribute ContolToValidate.You can also use a ValidationSummary control with the attribute FormToValidate, to validate all the input of a form. This way you can display a summary of errors instead of one error at the time.

Validation Controls ReferenceName FunctionCompareValidator Compares two valuesCustomValidator Provides custom validationRangeValidator Validates a rangeRegularExpressionValidator Validates an expressionRequiredFieldValidator Validates required dataValidationSummary Displays a validation summary

.NET Mobile Lists The Mobile List Control supports different input and display properties.

Selecting from a ListThis page has two forms:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><script runat="server">Sub Show_Price(sender As Object,e As ListCommandEventArgs)text1.Text=e.ListItem.Text & "=" & e.ListItem.ValueActiveForm=f2

7

Page 8: NET Mobile Tutorial

End Sub</script><Mobile:Form id="f1" runat="server"><Mobile:List runat="server"OnItemCommand="Show_Price"><Item text="Volvo" value="$30,000" /><Item text="BMW" value="$32,000" /><Item text="Audi" value="$34,000" /></Mobile:List></Mobile:Form><Mobile:Form id="f2" runat="server"><Mobile:Label runat="server" id="text1" /></Mobile:Form>

The first form has a list of cars.The second page displays a price. This page is activated when a car is selected on the first page.

.NET Mobile Selections The SelectionList Control supports drop down lists, check boxes and radio buttons.

The SelectionListThis mobile page uses a SelectionList to let the user select a car:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><script runat="server">Sub Car_Click(sender as Object, e as EventArgs) ActiveForm=f2 t1.text=cars.Selection.ValueEnd Sub</script><Mobile:Form id="f1" runat="server"><Mobile:SelectionList runat="server" id="cars" > <Item Text="Volvo" Value="$30,000" /> <Item Text="BMW" Value="$32,000" /> <Item Text="Audi" Value="$34,000" /></Mobile:SelectionList><Mobile:Command runat="server" OnClick="Car_Click" Text="Submit" /></Mobile:Form><Mobile:Form id="f2" runat="server"><Mobile:Label id="t1" runat="server" /></Mobile:Form>

When this page is displayed on a mobile device, the navigation and display functions of the page will be compiled differently for different devices with different display characteristics.Fore some devices, like a handheld PC, it might display a dropdown list to choose from. For a cell phone it might display a list of options to select from.

8

Page 9: NET Mobile Tutorial

.NET Mobile Images .NET Mobile displays different types of images for different types of devices.

The Image ControlDifferent mobile devices have different display capabilities.The Image Control allows the developer to specify different types of images for different types of devices.

Image TypesSome mobile devices will display GIF images. Other mobile devices will display BMP images or WBMP images. The Image Control allows you to specify different images for each preferred image type.This mobile page displays an image:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><Mobile:Form runat="server"><Mobile:Image runat="server"> <DeviceSpecific> <Choice ImageURL="image.gif" /> <Choice ImageURL="image.bmp" /> <Choice ImageURL="image.wbmp" /> </DeviceSpecific></Mobile:Image></Mobile:Form>

When this page is displayed on pocket PC, a GIF image will be displayed. On a cell phone a WBMP image or a BMP image will be displayed, according to the characteristics of the cell phone.

.NET Mobile Utilities

Utility controls support complicated user interfaces with minimum of code.

The AdRotator ControlThis mobile page displays different advertisements:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><Mobile:Form runat="server"><Mobile:AdRotator runat="server"AdvertisementFile="advertisements.xml"></Mobile:AdRotator></Mobile:Form> This is the ad file called "advertisements.xml":&br version="1.0" ?><?xml version="1.0" ?><Advertisements><Ad><ImageUrl>image1.gif</ImageUrl><BmpImageUrl>image1.bmp</BmpImageUrl><WBmpImageUrl>image1.wbmp</WBmpImageUrl><NavigateUrl>http://www.1.com</NavigateUrl><AlternateText>Visit 1</AlternateText></Ad>

<Ad>

9

Page 10: NET Mobile Tutorial

<ImageUrl>image2.gif</ImageUrl><BmpImageUrl>image2.bmp</BmpImageUrl><WBmpImageUrl>image2.wbmp</WBmpImageUrl><NavigateUrl>http://www.2.com</NavigateUrl><AlternateText>Visit 2</AlternateText></Ad><Ad><ImageUrl>image3.gif</ImageUrl><BmpImageUrl>image3.bmp</BmpImageUrl><WBmpImageUrl>image3.wbmp</WBmpImageUrl><NavigateUrl>http://www.3.com</NavigateUrl><AlternateText>Visit 3</AlternateText></Ad></Advertisements>

The Calendar ControlThis mobile page displays a calendar:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><script runat="server">Sub CalChanged(sender as Object,e as EventArgs)lab1.Text="You selected " & c1.SelectedDateActiveForm=f2End Sub</script><Mobile:Form id="f1" runat="server"><Mobile:Calendar id="c1" OnSelectionChanged="CalChanged" runat="server" /></Mobile:Form><Mobile:Form id="f2" runat="server"><Mobile:Label id="lab1" runat="server" /></Mobile:Form>

In this example a calendar is displayed in the first form. When the user select a date from the calendar, the selected date is displayed in a new form.

The PhoneCall ControlThis mobile page will display the text "Tove's number" and dial the number (555) 555-5555 when the user selects the text:<%@ PageInherits="System.Web.UI.MobileControls.MobilePage"%><%@ RegisterTagPrefix="Mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %><Mobile:Form runat="server"><Mobile:PhoneCall runat="server"PhoneNumber="(555) 555-5555"Text="Tove's number"AlternateFormat="{0}" /></Mobile:Form>

The attribute "AlternateFormat" has the value "{0}". This displays the text from the attribute "Text".If you use the value "{1}" it will display the text from the attribute "PhoneNumber".You can also use a construct like this: AlternateFormat="{0} is {1}". This will display the text "Tove's number is (555) 555-5555".

10

Page 11: NET Mobile Tutorial

Utility ControlsName FunctionAdRotator Displays advertisementsCalendar Displays a calendarPhoneCall Calls a telephone number

ASP.NET Mobile Controls Reference Main Mobile Objects

.NET Mobile supports three main objects: The Mobile Page The Mobile Form The Mobile Panel

The Mobile Page is the container for the application.Each Mobile Page can have one or more Mobile Forms.Each Mobile Form can have zero or more Mobile Panels.Mobile Forms and Mobile Panels are used to group Mobile Controls.

Mobile ControlsMobile Controls are grouped into:

User interface controls Validation controls Utility controls UI Controls

UI controls are controls that displays the user interface:Name FunctionCommand Performs an actionForm Defines a container for mobile controlsImage Defines an imageLabel Defines a textLink Defines a hyperlinkList Defines a listMobilePage Defines a base class for all mobile pagesObjectList Defines a list of data objectsPanel Defines a container for other controlsSelectionList Defines a list to select fromStyleSheet Defines styles to apply to other controlsTextBox Defines a single line input boxTextView Defines a multi-line input box

Validation ControlsValidation controls are used to validate the data entered by a user:Name FunctionCompareValidator Compares the value of one input control to the value

of another input control or to a fixed valueCustomValidator Allows you to write a method to handle the validation

of the value enteredRangeValidator Checks that the user enters a value that falls

between two valuesRegularExpressionValidator Ensures that the value of an input control matches a

specified patternRequiredFieldValidator Makes an input control a required fieldValidationSummary Displays a report of all validation errors occurred in a

page

11

Page 12: NET Mobile Tutorial

Utility Controls Utility controls support complicated user interfaces with minimum of code:Name FunctionAdRotator Displays advertisementsCalendar Displays a calendarPhoneCall Calls a telephone number

By: ZenonVBXSource: http://w3schools.com/dotnetmobile/default.asp

12