ASP.NET移动开发之SelectionList控件(4)
绑定数据源
下面这个示例将创建一个简单的ArrayList集合,作为Selection列表控件的数据源。在代码后置文件中,我们创建了一个Mobile类,用来存取每个数据项。在Page_Load事件处理函数中,我们将创建好的Mobile对象添加到一个ArrayList集合中。而后,将Selection列表控件与该ArrayList集合绑定。最后通过一个foreach 语句迭代整个列表,并将各个数据项中的信息以一个字符串的形式显示在页面上。
Default.aspx
Default.aspx.cs:
Mobile.cs:
http://hi.baidu.com/zhiwei%5F117/blog/item/1e34c926e8cb9909918f9d2b.html
下面这个示例将创建一个简单的ArrayList集合,作为Selection列表控件的数据源。在代码后置文件中,我们创建了一个Mobile类,用来存取每个数据项。在Page_Load事件处理函数中,我们将创建好的Mobile对象添加到一个ArrayList集合中。而后,将Selection列表控件与该ArrayList集合绑定。最后通过一个foreach 语句迭代整个列表,并将各个数据项中的信息以一个字符串的形式显示在页面上。
Default.aspx
+展开
-XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:Form id="Form1" runat="server">
<mobile:Label ID="Label1" Runat="server">请选择想要购买的手器品牌</mobile:Label>
<mobile:SelectionList ID="SelectionList1" Runat="server" SelectType="MultiSelectListBox" DataTextField="Manufacturer" DataValueField="Model">
</mobile:SelectionList>
<mobile:Command ID="Command1" Runat="server" OnClick="HandleMultiSelection">提交选择</mobile:Command>
</mobile:Form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label2" Runat="server">你选择的手机型号为:</mobile:Label>
<mobile:TextView ID="TextView1" Runat="server">TextView</mobile:TextView>
</mobile:Form>
</body>
</html>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:Form id="Form1" runat="server">
<mobile:Label ID="Label1" Runat="server">请选择想要购买的手器品牌</mobile:Label>
<mobile:SelectionList ID="SelectionList1" Runat="server" SelectType="MultiSelectListBox" DataTextField="Manufacturer" DataValueField="Model">
</mobile:SelectionList>
<mobile:Command ID="Command1" Runat="server" OnClick="HandleMultiSelection">提交选择</mobile:Command>
</mobile:Form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label2" Runat="server">你选择的手机型号为:</mobile:Label>
<mobile:TextView ID="TextView1" Runat="server">TextView</mobile:TextView>
</mobile:Form>
</body>
</html>
Default.aspx.cs:
+展开
-C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.MobileControls.MobilePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList array = new ArrayList();
array.Add(new MobileTelephone("Dopoda", "P800"));
array.Add(new MobileTelephone("Motorola", "A1200"));
array.Add(new MobileTelephone("Nokia", "N70"));
array.Add(new MobileTelephone("Samsung", "E638"));
SelectionList1.DataSource = array;
SelectionList1.DataBind();
}
}
protected void HandleMultiSelection(object sender, EventArgs e)
{
this.ActiveForm = Form2;
// Get the list items collection.
MobileListItemCollection colItems = SelectionList1.Items;
String strDisplaytext = "";
foreach (MobileListItem item in colItems)
{
if (item.Selected)
{
strDisplaytext += (item.Text + item.Value +
"<br/>");
}
}
TextView1.Text = strDisplaytext;
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.MobileControls.MobilePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList array = new ArrayList();
array.Add(new MobileTelephone("Dopoda", "P800"));
array.Add(new MobileTelephone("Motorola", "A1200"));
array.Add(new MobileTelephone("Nokia", "N70"));
array.Add(new MobileTelephone("Samsung", "E638"));
SelectionList1.DataSource = array;
SelectionList1.DataBind();
}
}
protected void HandleMultiSelection(object sender, EventArgs e)
{
this.ActiveForm = Form2;
// Get the list items collection.
MobileListItemCollection colItems = SelectionList1.Items;
String strDisplaytext = "";
foreach (MobileListItem item in colItems)
{
if (item.Selected)
{
strDisplaytext += (item.Text + item.Value +
"<br/>");
}
}
TextView1.Text = strDisplaytext;
}
}
Mobile.cs:
+展开
-C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// TeamStats 的摘要说明
/// </summary>
public class MobileTelephone
{
private String manufacturer, model;
public MobileTelephone(String manufacturer, String model)
{
this.manufacturer = manufacturer;
this.model = model;
}
public String Manufacturer { get { return this.manufacturer; } }
public String Model { get { return this.model; } }
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// TeamStats 的摘要说明
/// </summary>
public class MobileTelephone
{
private String manufacturer, model;
public MobileTelephone(String manufacturer, String model)
{
this.manufacturer = manufacturer;
this.model = model;
}
public String Manufacturer { get { return this.manufacturer; } }
public String Model { get { return this.model; } }
}
http://hi.baidu.com/zhiwei%5F117/blog/item/1e34c926e8cb9909918f9d2b.html
加支付宝好友偷能量挖...