|
Comments
|
Today's Top SOA Links
.NET Compact Framework Leveraging the List Controls in the Mobile Internet Toolkit
Leveraging the List Controls in the Mobile Internet Toolkit
By: Derek Ferguson
Dec. 16, 2002 12:00 AM
Microsoft's Mobile Internet Toolkit represents one of the best ways available today to create Web-based applications that target multiple kinds of mobile devices, including:
Some of the most powerful, yet least understood, of all the components in the Mobile Internet Toolkit are the various "list" objects. Of these, the first and simplest is simply known as the List control. A slightly more complex form of this control is the SelectionList. The absolute pinnacle of both power and difficulty-of-use, however, is the ObjectList control. In this article, I will give you a brief overview of the proper use of all three of these components.
The List List If you already know all of the items that you would like to have listed within this component, then you can set them all to this point using the Visual Studio Properties window. Within this window, there is an Items property. If you click this property, you will see a dialog like the one in Figure 1. To add items to the list using this interface, simply click the "Create New Item" button. Next, fill in the "Item Text" field, then the corresponding "Item Value" field. The difference between these two values is simple: the values in "Item Text" will actually be displayed to users of your Web application, whereas the values in "Item Value" are what will actually be sent to your code when one of these items is selected. To see an example of this, enter the names of several different Web sites as the "Text" values in your List control. For each of these Web sites, enter their URL as their "Value." For example, you might enter the Text of "SYS-CON Media homepage" and the Value of http://www.sys-con.com. Before running your application, make sure that you have also set the List's ItemsAsLinks property to true. Now when you run your application you will see that all of the Text properties you entered are displayed on the Web page as hyperlinks. If you follow any of these hyperlinks, however, you will be taken to the URL specified in the associated Value property! Rendering its Items as hyperlinks is the greatest degree of interactivity supported by the List control. It is intended principally for displaying output in an ordered fashion to a remote browser. There is, therefore, no way for the control to communicate back to your application once it has been rendered on the remote browser.
The SelectionList List To see the SelectionList in action, first delete your existing List component, then drag a SelectionList from the palette and drop it onto the place where your List used to be. Notice that there is an Items collection for this list component just like there was for the first List control. Go ahead and add a few Text/Value pairs. Notice that there is one feature on this dialog that was not on the dialog for the standard List control - the checkbox labeled "Selected." It is there because, unlike on the standard List control, items on the SelectionList may or may not be selected at any given point in time. In its default format, you can check this checkbox for as many items as you like. When you close the Items dialog, you will see that the SelectionList is now displaying the first selected item that you have entered. If you did not select any items, then it is simply displaying whatever the first item is that you entered. Scroll up to the SelectType property and try all of the various settings underneath this property (see Table 1). In Table 1, the first column (left to right) gives you the name of the SelectType option, as shown in the Properties dialog. The second column tells you the maximum number of items that will be displayed on the screen at a single time. For two of these - the Listbox and the MultiSelectListBox - this number is controlled by the Rows property, which is also visible in the Properties dialog. Exclusive versus nonexclusive selectivity will determine whether or not you can choose multiple items using a given SelectList control. If you want to do this, you must set the SelectType for your SelectList to either MultiSelectListBox or Checkbox, because none of the other SelectTypes support this. At this point, you should go ahead and add a Label and a Command control to your Mobile Web Form so that it looks like Figure 2. Double-click the SelectionList and enter the code shown in Listing 1. The SelectedIndex property will tell you the index of the first item that is selected in any SelectionList. Our code uses this property as an index into the SelectionList's Items collection so we can retrieve the Value of the first selected item. If you run your Mobile Web application project now, you will see that when you select an item, nothing happens. However, when you click the button, a round-trip to the server is triggered and the Label text is populated with the value of the first selected item. This raises two interesting questions. First, how can we retrieve the Values of all the items selected, rather than just the first one - in case we are using one of the two SelectTypes in which multiple items can be chosen? Second, how can we avoid having to click the command button in order to trigger a round-trip to the server? One approach to solving the first problem is shown in Listing 2. This code iterates through all of the items in a given SelectionList, checking the Selected property of each one. If this property is true, then it knows that the item has been selected and it concatenates its value to the end of the Caption string. At the end of the code, the entire caption string is pasted into the Label control.
The ObjectList List To begin with, the only way to get items into an ObjectList is to bind the ObjectList with a data source. You can verify this for yourself by deleting the Label, Command, and SelectionList from your existing Mobile Web Form and dragging an ObjectList onto the Mobile Web Form to replace them. If you examine the Properties window for this control, you will see that there is no Items property - therefore, there is no way to add items through the Properties window. The good news is that, unlike all of the other List controls, this control supports multiple fields. This means that if you want to list many pieces of information for each item, you should always choose the ObjectList control. For example, whereas with the List or SelectionList controls you could display only the names of all your employees, with the ObjectList, you could display their names, ages, salaries, and Social Security numbers all at once! In order to bind the ObjectList to a data source, you must first have a data source. This should be something that is accessible via ADO.NET, the new technology for accessing data using .NET. Examples of data that can be accessed via ADO.NET include:
Once you have a data source, you need to tell the ObjectList how to connect to it and what data it should use from it. You can do this by manipulating the properties described in Table 2. The last two rows in Table 2 are of particular interest because they tell us something about the functionality of the ObjectList, which is quite different from either of the previous lists. When an ObjectList is first displayed, it presents a simple list composed of a single field, just like any of the other lists. This is the field denoted by the LabelField property and should be a unique identifier for all of the items in your ObjectList. If you were displaying data about employees, for example, you might choose their names for this display, or their Social Security numbers. In this display, all of the items are links. In an HTML or cHTML browser, this would usually mean that they were all hyperlinks. On a WAP phone, it would probably mean that they were all items in a menu. Regardless of the form factor, they would all generate round-trips to the server immediately upon selection - unlike either of the two previous lists. Once the server received the selection, it would present a second view of the data that "punched down" onto additional data about the chosen record. The fields in this view would be pulled from the TableFields property. In addition to being able to display multiple data fields, the ObjectList is also alone in its ability to accept "commands" associated with each data item. This is possible via the Commands collection of the control, the Properties dialog for which is illustrated in Figure 3. To add commands, simply click the "Create New Command" button and then enter your desired command names and texts. At runtime, the texts for each of your commands will be displayed as selectable commands on the punch-down view for each item. In order to react to each of the commands, however, you will have to write some custom code. Listing 3 shows a general example of the kind of code that you will have to write. By double-clicking the ObjectList, you can bring up the ItemCommand event handler and note that it is passed an ObjectListCommandEventArgs object. This object has a number of useful properties, but here we use the CommandName property to determine which of the commands has been selected. The exact action you will want to perform will depend entirely upon the exact nature of your application and commands.
Conclusion Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||