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

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Chapter 10.


Syntax


The List control is always rendered with a trailing break, overriding any setting of the BreakAfter property. The LoadItems event and the ItemCount property are inherited from the PagedControl parent class; you use these only when you implement custom pagination. (See the section "Custom Pagination," later in the chapter, for more details.) You code the List control in server control syntax using the properties and values shown in the following listing.

<mobile:List
runat="server"
id="id"
Alignment="{NotSet|Left|Center|Right}"
BackColor="backgroundColor"
Font-Bold="{NotSet|False|True}"
Font-Italic="{NotSet|False|True}"
Font-Name="fontName"
Font-Size="{NotSet|Normal|Small|Large}"
ForeColor="foregroundColor"
StyleReference="StyleReference"
Wrapping="{NotSet|Wrap|NoWrap}"
DataMember="dataMember"
DataSource="dataSource"
DataTextField="DataTextField"
DataValueField="DataValueField"
Decoration="{None|Bulleted|Numbered}"
ItemsAsLinks="{False|True}"
ItemCount="itemCount"
OnItemDataBind="onItemDataBindHandler"
OnItemCommand="onItemCommandHandler"
OnLoadItems="loadItemsHandler">
<!-- Optional statically declared list items -->
<Item Text="Text" Value="Value" Selected="{True|False}" />
</mobile:List>


Properties and Events


Table 4-1 and Table 4-2 for details of those properties that are inherited from MobileControl.) The Type column describes the type of the property when you're setting or getting the property in code. Refer to the preceding "Syntax" section for valid values when setting the property in server control syntax.







































Table 6-3: Significant Properties and Events of the List Control


Property/Event


Type


Description


DataMember


String


Used only when the control is data bound to a DataSet or DataTable object. This attribute specifies the name of the table in the DataSet object to which the control should bind.


DataSource


Object


When the control is data bound, DataSource identifies the collection object or DataSet that is the data source.


DataTextField


String


When the control is data bound, either to a DataSet object or to an enumerated collection, DataTextField specifies the name of the field in the data source that is displayed in the list.


DataValueField


String


When the control is data bound, either to a DataSet object or to an enumerated collection, DataValueField specifies the name of the field in the data source that provides the hidden data value associated with each item in the list.


Decoration


System.Web. UI.MobileControls.Lis tDecoration enumeration: None|Bulleted| Numbered


On HTML browsers, Decoration dictates the presentation style used. Default is ListDecoration.None.


ItemsAsLinks


False|True


Used in special cases in which you use the Text value of each list item for the hyperlink text and the value is a valid URI. When you select ItemsAsLinks, the client directly calls the specified resource, meaning that the code can't deliver any selection events. Consequently, setting this attribute to True overrides the OnItemCommand property.


ItemCount


Integer


You use this property with custom pagination. ItemCount specifies the total number of items in the source dataset. To use custom pagination, you must set the Form.Paginate property to True.


ItemCommand (event)


Event handler method name


Specifies the event handler to call when a user selects an item in the list—except when you've specified ItemsAsLinks, as described above.


LoadItems (event)


Event handler method name


Required when you've specified an ItemCount property, thus enabling custom pagination. The code calls this event handler each time the runtime requires new data. This allows you to pass data to the control in chunks.



Using the List Control


As mentioned at the beginning of this section, you can use the List control in two ways:



Non-interactive modeIn this mode, items are rendered as a simple display list. The user can't select any items. On HTML browsers, the list is rendered in the style indicated by the Decoration property. For this mode, do not define an ItemCommand event handler.



Interactive modeThis mode activates if you define an ItemCommand event handler. In this case, the Text value of each list item is rendered as a selectable link that calls the ItemCommand event handler when the user activates it.



Setting ItemsAsLinks to True creates a unique situation that overrides this behavior. This attribute causes the list to be rendered as a set of hyperlinks. The Text property of each list item becomes the hyperlink text, while the Value property identifies the destination. When the user selects such a link, the client calls for a resource at the URL specified, meaning that the code doesn't make a call to the ItemCommand event handler.

You can use the List control with the <Item> tag with both the statically defined list items by using the <Item> element or by data binding to a single field of a data collection using the DataMember, DataSource, DataTextField, and DataValueField elements. You program these features for the List control the same way you do for the SelectionList control. Refer to the section "The SelectionList Control" earlier in this chapter for details.

Trapping User Selections


When you use the ItemCommand property to specify an event handler to call when the user selects a list item, the List control operates just like a SelectionList control in single-selection mode. Although the List control doesn't support the same presentation options as SelectionList, it does support pagination for large lists. In addition, the List control causes a postback from the client to the server, rather than requiring an accompanying Command control, as the SelectionList control does.

The second argument of the ItemCommand event handler is a System.Web.UI.MobileControls.ListCommandEventArgs object, which contains a ListItem property that identifies the item that the user selects. Listings 6-7 and 6-8 depict a new version of the team statistics example we used earlier to demonstrate the SelectionList control in single-selection mode. However, here we've updated the code to use the ItemCommand functionality of the List control.

Listing 6-7: Source for ListItemCommandExample.aspx






<%@ Page Inherits="MSPress.MobWeb.ListItmCmd.MyWebForm" Language="c#" 
CodeBehind="ListItemCommandExample.aspx.cs"%>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<mobile:Form runat="server" id="Form1">
<mobile:Label runat="server" id="Label1" StyleReference="title">
Season 2003 results
</mobile:Label>
<mobile:Label runat="server" id="Label2">Select a team:</mobile:Label>
<mobile:List runat="server" id="List1">
<item Text="Dunes" Value="Posn:1 Pl:38 Pts:80"/>
<item Text="Phoenix" Value="Posn:2 Pl:38 Pts:70"/>
<item Text="Eagles" Value="Posn:3 Pl:38 Pts:69"/>
<item Text="Zodiac" Value="Posn:4 Pl:38 Pts:68"/>
</mobile:List>
</mobile:Form>
<mobile:Form runat="server" id="Form2">
<mobile:Label runat="server" id="Label3" StyleReference="title">
Team Full Stats:
</mobile:Label>
<mobile:Label runat="server" id="Label4" />
</mobile:Form>











Listing 6-8: Code-behind file ListItemCommandExample.aspx.cs






using System;
using System.Web.UI.MobileControls;
namespace MSPress.MobWeb.ListItmCmd
{
public class MyWebForm : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.List List1;
protected System.Web.UI.MobileControls.Label Label4;
protected System.Web.UI.MobileControls.Form Form2;
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.List1.ItemCommand += new
ListCommandEventHandler(this.ClickTeamSelection);
}
private void ClickTeamSelection(
Object source,
ListCommandEventArgs args)
{
// Display the Stats page
this.ActiveForm = Form2;
String strSelectedTeamStats = args.ListItem.Value;
Label4.Text = args.ListItem.Text
+ ": " + strSelectedTeamStats;
}
}
}











Instead of requiring a separate Command control to post results to the server, each item in the list is now a link, as Figure 6-5 illustrates.


Figure 6-5: List control example output

Automatic Pagination


As shown in Figure 6-1, unlike the SelectionList control, the List control derives from the PagedControl class. This gives the List control support for internal pagination. To use this feature, you must supply all the data to the control up front and set the Paginate property of the enclosing Form control to True.

If you enable automatic pagination, the Mobile Internet Controls Runtime will insert page breaks between controls to split the output over the necessary number of screens, depending on the client's capabilities. For controls that support internal pagination, such as the List and ObjectList controls, automatic pagination permits the runtime to insert page breaks between list items.


Custom Pagination


Instead of supplying all the data up front, you can pass data to the control on demand each time a new display page builds. You can activate custom pagination by setting the ItemCount property to the total number of items that can be displayed across all pages. The control paginates as though it had all the data, even though you didn't supply any data initially. Then, as each page is constructed, the control raises the LoadItems event that you can trap in code in your event handler. In your event handler, you can fetch the data and supply the next page of items to the control for display. This technique can yield performance benefits when the list is very large or in situations where the computational effort required is high.

Your LoadItems event handler method takes a parameter of type System.Web.UI.MobileControls.LoadItemsEventArgs. This object has two properties that specify how much data to return:



ItemIndexThe index of the first item



ItemCountThe number of items to return



For example, if ItemCount is 10 and ItemIndex is 50, the event handler must supply 10 items starting from (and including) the 50th record in the data source. You build the items that you want to display as objects of type MobileListItem, and you add them to the Items collection of the List control, as Listings 6-9 and 6-10 demonstrate. In this example, the code creates an array of TeamStats objects that provides the data source. Keep in mind that a real application would probably be getting data from a database or some external data source. Each time the application requires more display data, it calls the LoadTeams event handler.





Important

Clear the Items collection of the List object each time the application makes a call, as this sample does in the first line of the LoadItems method; otherwise, the runtime will attempt to display the same items it showed in the previous screen as well as the ones you've added to the collection.


Listing 6-9: Source for CustomPaginationExample.aspx






<%@ Page Inherits="MSPress.MobWeb.CusPag.ExampleWebForm" Language="c#" 
CodeBehind="CustomPaginationExample.aspx.cs" AutoEventWireup= "False" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<mobile:Form runat="server" id="Form1" paginate="true">
<mobile:Label runat="server" StyleReference="title">
Season 2003 results</mobile:Label>
<mobile:List id="List1" runat="server"></mobile:List>
</mobile:Form>











Listing 6-10: Code-behind file CustomPaginationExample.aspx.cs






using System;
using System.Collections;
using System.Web.UI.MobileControls;
namespace MSPress.MobWeb.CusPag
{
public class ExampleWebForm : System.Web.UI.MobileControls.MobilePage
{
private TeamStats[] _premierTable;
protected System.Web.UI.MobileControls.List List1;
public ExampleWebForm()
{
// In the constructor, create the data source we will use.
_premierTable = new TeamStats[16];
_premierTable[0] = new TeamStats("Dunes", "Pts:80");
_premierTable[1] = new TeamStats("Phoenix", "Pts:70");
_premierTable[2] = new TeamStats("Eagles", "Pts:69");
_premierTable[3] = new TeamStats("Zodiac", "Pts:68");
_premierTable[4] = new TeamStats("Arches", "Pts:66");
_premierTable[5] = new TeamStats("Chows", "Pts:61");
_premierTable[6] = new TeamStats("Creation", "Pts:57");
_premierTable[7] = new TeamStats("Illusion", "Pts:54");
_premierTable[8] = new TeamStats("Torpedo", "Pts:52");
_premierTable[9] = new TeamStats("Generals", "Pts:52");
_premierTable[10] = new TeamStats("Reaction","Pts:51");
_premierTable[11] = new TeamStats("Peanuts", "Pts:49");
_premierTable[12] = new TeamStats("Caverns", "Pts:48");
_premierTable[13] = new TeamStats("Eclipse", "Pts:42");
_premierTable[14] = new TeamStats("Dragons", "Pts:42");
_premierTable[15] = new TeamStats("Cosmos", "Pts:42"); }
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.List1.LoadItems +=
new LoadItemsEventHandler(this.LoadTeams);
}
private void Page_Load(Object sender, EventArgs e)
{
// Tell the List how many items it can expect by the time
// it has asked for them all.
List1.ItemCount = _premierTable.Length;
}
private void LoadTeams(Object source, LoadItemsEventArgs args)
{
List1.Items.Clear();
// The LoadItemsEventArgs tells us which items and how many.
for (int i = 0; i < args.ItemCount; i++)
{
// Get the relevant item from the array;
// Create a MobileListItem.
int intTablePosn = args.ItemIndex + i;
MobileListItem lstItem = new MobileListItem(
string.Format("{0} {1}",intTablePosn+1,
_premierTable[intTablePosn].TeamName),
_premierTable[intTablePosn].Stats);
// Add the item to the Items collection of the List control.
List1.Items.Add(lstItem);
}
}
}
class TeamStats
{
private String teamName, stats;
public TeamStats(String teamName, String stats)
{
this.teamName = teamName;
this.stats = stats;
}
public String TeamName
{ get { return this.teamName; } }
public String Stats
{ get { return this.stats; } }
}
}











If you run the code in these two listings with Internet Explorer, all the items will be displayed on a single screen. However, using a device with a small display screen causes the output to paginate, as Figure 6-6 shows.


Figure 6-6: Custom pagination in action on the Nokia Simulator





Tip

You can override the default text for the Next and Previous buttons used for navigation by setting the NextPageText and PrevPageText properties of the Form control's PagerStyle object, as shown here:


    this.Form1.PagerStyle.NextPageText = "->";

/ 145