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

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Overriding Single-Field Display in the List Controls

You usually use the SelectionList and List controls to bind to only two properties of the data item: the displayed property set by the DataTextField property of the List control, and the hidden value field set by the DataValueField property.

The ObjectList control provides the TableFields property, through which you can specify more than one property to display in each row of the initial display list, provided that the client device has an HTML browser and has a large enough screen (such as a Pocket PC). However, on devices with a small screen, such as a mobile phone with a Wireless Markup Language (WML) browser, the ObjectList control is still rendered as a list consisting of a single property from the data source.


Overriding Single-Field Display in SelectionList and List Controls


You can make the SelectionList and List controls display more than one property by creating an OnItemDataBind event handler, in which you set the ListItem.Text property to a string that you build by concatenating the values of two or more individual fields.

Returning to the example shown in Listings 10-1 and 10-2, you add code to wire up the ItemDataBind event handler method in the InitializeComponent method, as shown in the following code. (Or you can use one of the other techniques described in Chapter 3.)

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.List1.ItemCommand += new
ListCommandEventHandler(this.List1_OnItemCommand);
this.List1.ItemDataBind += new
ListDataBindEventHandler(this.List1_OnItemDataBind);
}

You can then code the event handler method as shown here:

private void List1_OnItemDataBind(
Object sender,
ListDataBindEventArgs e)
{
e.ListItem.Text = String.Format ("{0} : {1}",
((TeamStats)(e.ListItem.DataItem)).Position,
((TeamStats)(e.ListItem.DataItem)).TeamName);
}

This code will cause the initial list to display a composite item composed of the Position and TeamName fields, rather than just the TeamName field. Figure 10-2 shows how this list appears on the Nokia simulator and a Pocket PC. The C# and Visual Basic samples ListDisplayMultipleFieldsExample in the companion material on this book's Web site are simple applications that use this technique.


Figure 10-2: OnItemDataBind method implemented to override default display of a single property from a List control's data source


Overriding Single-Field Display in ObjectList


As we've just seen, to override single field display in the SelectionList and List controls, you implement an OnItemDataBind event handler method and set the Text property of the MobileListItem object to the new value you want to display. The ObjectList control's ObjectListItem object is more complex than the MobileListItem. The ObjectList control displays a single field from the data source in the initial list that displays and then displays many more fields from the source data in an item's Details view. To support this functionality, the ObjectListItem contains a collection of fields that represents each of these data fields, indexed by the field name. When using an OnItemDataBind method, you must name the field from this collection you want to reset. The following code presents an example of an OnItemDataBind method for an ObjectList control that resets the display of the TeamName field, making the new value to be displayed a composite of the values of the Position and TeamName fields from the data source:

private void ObjectList1_OnItemDataBind(
Object sender,
ObjectListDataBindEventArgs e)
{
// Get the data object being bound.
TeamStats dataObj = (TeamStats)e.DataItem;
// Get the list item being created.
ObjectListItem item = (ObjectListItem)e.ListItem;
// Modify the text displayed for a field.
item["Posn-Team"] =
String.Format ("{0} : {1}", dataObj.Position, dataObj.TeamName) ;
}

This code extract is taken from the sample application ObjectListDisplayMultipleFieldsExample, which is available in the companion material on this book's Web site.

/ 145