Getting Started with Mobile Web Forms
Just as with ASP.NET pages, mobile pages consist of Web forms and server controls. Recall that ASP.NET pages can have server controls, script blocks, and HTML. In a mobile Web page, however, only server controls and script blocks are allowed; they cannot contain any HTML.
Let's take a quick look at a short example, shown in Listing 23.1.
Listing 23.1 Your First Mobile Page
1: <%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="VB" %>
2: <%@ Register TagPrefix="Mobile" Namespace="System.Web.UI.MobileControls"
4: <Mobile:Form BackColor="#ccddcc" runat="server"
5: FontName="Times" FontSize="3">
6: <Mobile:Label runat="server" Text="Hello World!"/>
Save this listing as
. At first glance, this looks like any other ASP.NET page that inherits from a code-behind (Day 19, "Separating Code from Content") and implements a
control (Day 6, "Learning More About Web Forms"). Recall that the
attribute on line 1 specifies a class that this page should inherit from (instead of the usual
class). All mobile pages must inherit from this class so that ASP.NET
that you're building a special type of ASP.NET page.
Line 2 looks like the registration for a user control. In essence, that's what it is. The namespace
contains all the mobile server controls that you'll be using in your mobile pages—it resides in a file named
. This means that mobile server controls are not directly part of ASP.NET; you have to register them explicitly. Note the
; I'll touch on this
Lines 1 and 2 highlight the two lines of code that
be in every mobile Web page:
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="VB"%>
<%@ Register TagPrefix="Mobile" Namespace="System.Web.UI.MobileControls" %>
Don't forget these lines in your pages!
On line 4, you start a mobile Web form, which is similar to an ASP.NET Web form, except that instead of using the tag prefix
, you use
, as declared on line 2. Note that this form also must have the
attribute, which gives it the same capabilities as regular Web forms. In the declaration of this form, you specify a few additional properties, such as
here are used to reflect what a
phone user would see on her LCD screen).
On line 6, you declare a mobile
control, which works just like a regular
control. Note that it also uses the
tag prefix, and contains the
Finally, on line 7, you close the mobile Web form. When
from the browser, this listing produces Figure 23.3.
Figure 23.3. The page as shown from a cell phone…
You'll examine more of the mobile server controls later today, but they are typically very similar to their counterparts in ASP.NET. Table 23.1 shows all the controls available to mobile Web forms.
Table 23.1. The Mobile Web Server Controls
Displays ads specified from an XML advertisement file.
Displays an interactive calendar.
Provides a way to invoke event handlers; similar to a button in ASP.NET.
Compares the value in one control to a constant value or another control.
Allows you to provide your own custom validation routines.
Used for templates on mobile Web forms.
A container for controls in a mobile page.
Displays an image.
Creates a hyperlink to another form or page.
Renders a list of items.
The base class for all mobile ASP.NET pages.
Similar to a
control, but lists data types.
A container for other mobile controls.
Displays a device-dependent user interface for automatically calling or displaying telephone
Validates the value in a control against a specified range of values.
Validates the value in a control against a regular expression string.
Ensures that a value is entered into a control (that is, ensures that the control is not left blank).
Similar to the ASP.NET
Used to organize styles for controls; does not provide a visual display.
Displays a single-line text box for user entry. Use the
property to determine which kind of box to display:
(hides input with
(restricts input to numeric values, useful for cell phones).
Displays a large amount of noneditable text. Similar to the
Displays a summary of all validation errors that have occurred on the page.
Note that many of the properties of these controls are dependent on the viewing device. For example, setting the
property on a
should theoretically allow only numbers to be entered as input. In a Web browser, this restriction is not enforceable without complex client-side scripting. It
enforceable, however, in cell phones. Take note of your viewing audience (see "Device-Specific Output and Template Sets," later today).
The Way Mobile Forms Work
Aside from a few small changes, Listing 23.1 looks and works just as an ASP.NET page does. Mobile forms can, however, get a bit more complex.
Let's examine the design of mobile devices a bit further. If you've ever used a cell phone or similar device, you know that interacting with one is not exactly the same as interacting with a PC. Due to the small
of the screen and the form of the input devices, applications must present information in a different way. Specifically, information is gathered into small
that can be displayed on a few "pages" of the device. There isn't a lot of text to display, there are hardly any complex images, and users make choices by selecting from a menu. Compare this to Web pages, where text is a very prominent feature, images are abundant, and most user interaction is accomplished by clicking on links or
, and you can see why a different paradigm is needed.
Also, navigation in mobile devices isn't as easy as in a browser; users can't simply type in the URL to a new page, and loading new pages often takes a very long time due to the limited bandwidth of these devices.
To address this, mobile Web forms have several differences from ASP.NET Web forms. The first, and probably largest, is that mobile pages can contain as many server-side forms as necessary, whereas ASP.NET pages can only contain one. Each form can contain different information and controls to display; this minimizes the need to load new pages into the device. A single page can post back onto itself, and depending on the criteria, the page can choose to display a different form. Thus, a mobile Web form and a mobile page are two highly
entities. A single mobile page can contain an entire application's worth of mobile forms. Figure 23.4 illustrates the use of multiple forms on one mobile page.
Figure 23.4. An entire application can be contained in one mobile page, but several mobile forms.
Mobile Web forms also focus more on presenting information to the user as menus and lists, rather than long paragraphs of text and links.
and lists are the
elements for users to interact with in the mediocre input controller world of wireless devices, so it is only logical that emphasis would be placed on these constructs.
There are other minor differences that you'll come across as you're developing your pages. There are
similarities as well, such as event handling and user input validation, which work the same as with ASP.NET pages. Let's build some more examples.
Building a More Apropos Interface
Before I go into more examples, though, you might have noticed that Figure 23.3 isn't exactly the ideal interface for developing mobile applications. The screen might be too large to show developers what their pages will actually look like. Therefore, let's develop a different interface, one that better reflects the target audience.
Listing 23.2 shows a page that
your mobile pages in an Iframe—a type of HTML frame that can contain different URLs within the containing page.
Listing 23.2 Developing a Better Suited Interface
1: <script Language="VB" runat="server">
2: sub Page_Load(Sender as Object, e as EventArgs)
3: frmScreen.Attributes("src") = Request.Params("page")
4: end sub
7: <p> <p>
9: <table border=1 width=100 height=100>
11: <td valign="top">
12: <iframe id="frmScreen" width="125" height="100"
13: marginwidth="2" marginheight="2"
Save this listing as
. This page is mostly plain HTML that displays an IFrame, shown on line 12. The IFrame does not map to a predefined ASP.NET server control, but that doesn't stop you from making it into a generic control (represented by the
; see Appendix C, "ASP.NET Controls: Properties and Methods," for more information). Notice that the
property, which determines which page to display in the IFrame, is set to the value returned from a
parameter on line 3. This code retrieves a variable named
from the querystring—this variable holds the actual mobile page you want to display.
Now request this page from your browser with the following URL:
Note that the querystring contains the mobile page you developed earlier. This URL should produce the display shown in Figure 23.5.
Figure 23.5. The IFrame allows you to display a mobile page in a way more representative of your target audience.
Now that looks more like it! It's not exactly a representation of a cell phone, but this will allow you to better see how your pages will look in smaller devices. The rest of the examples in this chapter use
with a particular querystring to display the output.