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

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


اندازه قلم

+ - پیش فرض

حالت نمایش

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

Using ASP.NET Declarative Data Binding

Many of the examples introduced in earlier chapters in this book included data expressions in the mobile Web Forms page, enclosed in these tags: <%#…%>. This ASP.NET declarative data binding syntax is particularly useful in templates when you want to access data items in the underlying data collection. However, you can also use this syntax to achieve the following results:

Insert the values of public variables, page properties, or other controls into your Web page

Specify the data collections to which controls are bound

Call methods or evaluate expressions

Table 11-1 offers some examples of common uses for this data binding syntax. Listings 11-1 and 11-2 show many of these techniques in action.

Table 11-1: Examples of Declarative Data Binding Syntax

Source of Data

Example Usage



<%# TopTitle %>

The value that displays is the TopTitle property of the code-behind class.


LabelField="TeamName "
DataSource =
<%# MyArray %>

The DataSource property of the ObjectList is set to the MyArray property of the code-behind class. The MyArray property exposes an instance of a collection class (such as ArrayList) or DataTable.


<%# (TeamStats.Played 
+ " Pts: "
+ TeamStats.Points) %>

The value that displays is constructed from an expression that concatenates properties of the TeamStats class and literal text.

Function execution

<%# String.Format(
"Position: {0}",
PadLeft(2,'0')) %>

Here the content of the page is the output of the String.Format function.

Method result

<%# GetOdds(SelectionLis t1
.Selection.Text) %>

In this example, the GetOdds method is a method of the code-behind class. The return value of this method is inserted into the mobile page.

You can use declarative data binding code anywhere in a mobile Web Forms page, as long as the evaluated expression returns the correct object type for the context it's used in. For example, in the Collection item in Table 11-1, the MyArray variable must evaluate to an object that implements IEnumerable or IListSource.

To evaluate data binding expressions, you must call the DataBind method of the containing control. Usually, calling the DataBind method of the mobile page within the Page_Load event handler is sufficient because doing so also calls the DataBind method of all enclosed controls. The following code illustrates this concept:

protected void Page_Load(Object sender, EventArgs e)

Sometimes calling the DataBind method of the MobilePage class isn't appropriate. If the data binding expressions reference objects that have a null value when the page first loads, calling the MobilePage.DataBind method this way will cause a run-time error. For example, referencing the Selection property of a SelectionList or an ObjectList control before the user makes a selection causes a run-time error. In this case, you might have to delay calling DataBind for all objects in the mobile page until you've defined the required items, as the following example demonstrates.

Listings 11-1 and 11-2 show an example application that uses an ObjectList control and declarative data binding wherever possible.

Listing 11-1: Source file DeclarativeDataBindingExample.aspx

<%@ Page Inherits="MSPress.MobWeb.DeclDBEx.ExampleWebForm" Language="c#" 
CodeBehind="DeclarativeDataBinding.aspx.cs" AutoEventWireup="false" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<mobile:Form runat="server" id="Form1">
<mobile:Label id="Label1" runat="server" StyleReference="title">
<%# TopTitle %></mobile:Label>
<mobile:ObjectList id="ObjectList1" runat="server"
DataSource = <%# MyArray %> >
<Command Name="aSelection" Text="Show Details"/>
<mobile:Form runat="server" id="Form2">
<mobile:Label id="Label2" runat="server" StyleReference="title">
You selected <%# ObjectList1.Selection["TeamName"] %>
<mobile:TextView id="txvDetail" runat="server">
Played : <%# ObjectList1.Selection["Played"] %> <br>
Points : <%# ObjectList1.Selection["Points"] %> <br>
<%# String.Format("Position: {0}",
ObjectList1.Selection["Position"].PadLeft(2,'0')) %>

Listing 11-2: Code-behind file DeclarativeDataBindingExample.aspx.cs

using System;
using System.Collections;
using System.Web.UI.MobileControls;
namespace MSPress.MobWeb.DeclDBEx
public class ExampleWebForm : MobilePage
protected System.Web.UI.MobileControls.Form Form1;
protected System.Web.UI.MobileControls.Form Form2;
protected System.Web.UI.MobileControls.ObjectList ObjectList1;
private ArrayList _myArray;
protected ArrayList MyArray
get { return _myArray; }
public string TopTitle
get { return "Season 2003 results"; }
override protected void OnInit(EventArgs e)
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);
this.ObjectList1.ItemCommand += new
private void Page_Load(Object sender, EventArgs e)
if (!this.IsPostBack)
_myArray = new ArrayList();
_myArray.Add(new TeamStats("Dunes",1,38,24,8,6,80));
_myArray.Add(new TeamStats("Phoenix",2,38,20,10,8,70));
_myArray.Add(new TeamStats("Eagles",3,38,20,9,9,69));
_myArray.Add(new TeamStats("Zodiac",4,38,20,8,10,68));
private void OnTeamSelection(
Object source,
ObjectListCommandEventArgs args)
this.ActiveForm = Form2;
class TeamStats
private String _teamName;
private int _position, _played, _won, _drawn, _lost, _points;
public TeamStats(String teamName,
int position,
int played,
int won,
int drawn,
int lost,
int points)
this._teamName = teamName;
this._position = position;
this._played = played;
this._won = won;
this._drawn = drawn;
this._lost = lost;
this._points = points;
public String TeamName { get { return this._teamName; } }
public int Position { get { return this._position; } }
public int Played { get { return this._played; } }
public int Won { get { return this._won; } }
public int Drawn { get { return this._drawn; } }
public int Lost { get { return this._lost; } }
public int Points { get { return this._points; } }

In Listing 11-2, the ExampleWebForm class gains two new public properties: a TopTitle string, and an ArrayList named MyArray. MyArray allows access to the private class member _myArray, which we've set up in the Page_Load event handler. In Listing 11-1, we bind the text value of the Label1 label to the TopTitle property of the ExampleWebForm class in the code-behind module. We then access the MyArray property to provide the data source collection for the ObjectList control.

When the user selects an item from the list, the code calls the OnTeamSelection event handler. This sets the active form to Form2, in which you use further data binding expressions to access the Selected property of the ObjectList situated on the first form.

Note carefully the use of DataBind here. In the Page_Load method, we call DataBind only for Form1 and, by implication, all controls that the form contains. If the application calls DataBind for the MobilePage at this point, a run-time error occurs because Form2 contains data binding statements that reference the ObjectList1.Selected property, which remains null until the user makes a selection. The code resolves the data binding expressions for the second form by calling Form2.DataBind from within the OnTeamSelection event handler, which occurs after the user makes a selection.

/ 145