ASP.NET移动开发之讲解SelectionList控件
|
源代码网推荐 源代码网推荐 SelectionList控件的列表中只有一个可视的数据项,其它的数据项只能以隐藏值的形式与可视的数据项进行关联。要在服务器控件语法中指定隐藏值,可以在<Item>元素中使用Value属性,并且将Value属性指定某数据项即可。如果使用动态绑定的形式来构建列表的话,那么你可以使用DataValueField属性指定数据源中的某个字段作为隐藏值。 源代码网推荐 源代码网推荐 语法 源代码网推荐 源代码网推荐 SelectionList列表控件的语法如下面的清单所示: 源代码网推荐 源代码网推荐 <mobile:SelectionList 源代码网推荐runat="server" 源代码网推荐id="id" 源代码网推荐Alignment="{NotSet|Left|Center|Right}" 源代码网推荐BackColor="backgroundColor" 源代码网推荐BreakAfter="{True|False}" 源代码网推荐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" 源代码网推荐SelectType="{DropDown|ListBox|Radio|MultiSelectListBox|CheckBox}" 源代码网推荐Title="String" 源代码网推荐OnItemDataBind="itemDataBindHandler" 源代码网推荐OnSelectedIndexChanged="selectedIndexChangedHandler"> 源代码网推荐 源代码网推荐<!-- 可选,以静态的方式声明数据项--> 源代码网推荐<Item Text="Text" Value="Value" Selected="{True|False}"/> 源代码网推荐 源代码网推荐</mobile:SelectionList> 源代码网推荐 至于要显示的列表数据项我们可以从数据源中进行读取,这种情况下我们需要使用DataMember、 DataSource、DataTextField和DataValueField等属性。当然你也可以使用<item>标签静态地定义要显示的数据项和隐藏值。注意,在上述SelectionList列表控件的语法中,并没有包含SelectedIndex这个属性,这是因为我们不可以在服务器控件语法中使用它,只有通过代码才可以使用SelectedIndex属性来获取当前选项的索引值。如果在服务器控件语法中要使某数据项处于被选中的状态,你可以在与该数据项对应的<Item> 标签中设置Selected属性为True。 源代码网推荐 源代码网推荐 属性和事件 源代码网推荐 源代码网推荐 下表描述列举了SelectionList列表控件中的一些常用的属性和事件,其中"类型"列描述了对应属性的类型,这样你就可以在代码中对这些属性进行设置和读取,至于这些属性可用的值你可以参考"语法"小节中的说明。 源代码网推荐 源代码网推荐
Selection列表控件的类型
源代码网推荐 构建一个静态列表 源代码网推荐 源代码网推荐 在静态列表中,数据项是使用Text属性指定一个字符串来进行呈现的,而不是动态地读取自数据源中的某个字段内容。要指定一个静态列表的数据项,你必须使用<Item>属性,具体的示例代码如下: 源代码网推荐 源代码网推荐<Item Text="Text" Value="Value" Selected="{True|False}" /> 源代码网推荐 源代码网推荐 Text属性用来指定在列表中显示的数据项信息,而Value就是相关联的隐藏值。如果你希望某数据项在默认情况下就处于被选中的状态的话,那么你可以将Selected属性设置为True。 源代码网推荐 源代码网推荐 注意,每个Selection列表控件都与一个MobileListItemCollection对象关联。当你使用服务器控件语法静态地定义数据项,实际上是将与每个数据项对应的MobileListItem对象添加到MobileListItemCollection对象中。你可以在代码中使用Items属性来访问MobileListItemCollection集合。你还可以使用MobileListItemCollection类中的Add、Clear和Remove等方法来添加、清除或删除数据项。这些方法的具体用法你可以参考相应的MSND帮助文档,这里就不再详细介绍了。 源代码网推荐 源代码网推荐 识别Selection列表控件中被选中的数据项(单项模式) 源代码网推荐 源代码网推荐 在Selection列表控件的单选模式下,你可以使用Selection.Name属性来获取被选中数据项的显示文本,还可以使用Selection.Value属性获取被选中数据项的对应的隐藏值。下面的两个程序清单就是一个显示被选中数据项对应隐藏值的示例代码: 源代码网推荐 源代码网推荐 Default.aspx: 源代码网推荐 源代码网推荐 <%@ 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"> 源代码网推荐<Item Text="Dopoda" Value="P800" /> 源代码网推荐<Item Text="Motorola" Value="A1200" /> 源代码网推荐<Item Text="Nokia" Value="N70" /> 源代码网推荐<Item Text="Samsung" Value="E638" /> 源代码网推荐 </mobile:SelectionList> 源代码网推荐 <mobile:Command runat="server" id="Command1" 源代码网推荐OnClick="HandleSingleSelection">提交选择</mobile:Command> 源代码网推荐</mobile:Form> 源代码网推荐<mobile:Form ID="Form2" Runat="server"> 源代码网推荐<mobile:Label runat="server" id="Label2">你选择的手机型号为:</mobile:Label> 源代码网推荐<mobile:Label runat="server" id="Label3"/> 源代码网推荐</mobile:Form> 源代码网推荐</body> 源代码网推荐</html> 源代码网推荐 程序代码说明:在这个程序中,我们在该页面上创建了两个窗体,Form1上放置了一个SelectionList列表控件,默认的类型为下拉列表,所以你只可以选择一项。当然,你还可以显式地设置SelectType属性为"DropDown"。在代码中可以看到,我们使用<Item>元素静态地定义各个数据项,其中用Text属性设置列表控件要显式的字符信息,而Value属性就是与显示的字符信息相关联的隐藏值(本例中为手机的制造商和机型)。由于你选择一个数据项后,SelectionList列表控件无法自动将改变后的信息自动回发到服务器上,所以还在Form1上再放置了一个Command控件,用来产生一个回发。也就是说当用户选择好了列表中的某项后,在点击提交按钮就可以将相应的信息回发到服务器端进行处理了,我们在本例中设置了该Command控件OnClick事件的处理函数为HandleSingleSelection,该事件处理函数的具体内容你可以查看对应的代码后置文件。Form2的功能就是显示那个数据项被用户选中,当你点击提交按钮后,Form2窗体将作为活动窗体,显示信息。 源代码网推荐 源代码网推荐 Default.aspx.cs 源代码网推荐 源代码网推荐 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 HandleSingleSelection(object sender, EventArgs e) 源代码网推荐{ 源代码网推荐this.ActiveForm = Form2; 源代码网推荐String selectedMobile = SelectionList1.Selection.Value; 源代码网推荐Label3.Text = SelectionList1.Selection + selectedMobile; 源代码网推荐} 源代码网推荐} 源代码网推荐 程序代码说明:HandleSingleSelection就是Command控件的OnClick事件的处理函数。一旦用户点击了该Command按钮,将使Form2成为活动窗体。并在Label3控件上将选中的数据项的字符信息和隐藏值显示出来。 源代码网推荐 源代码网推荐 如果你不在移动Web.Config文件中设置如下的代码,则在Openwave浏览器中会出错: 源代码网推荐 源代码网推荐 <sessionState mode="InProc" cookieless="true" timeout="20"></sessionState> 源代码网推荐 为此你最好将如下的移动Web.Config配置代码替代Visual Studio自动生成的移动Web.Config配置代码: 源代码网推荐 源代码网推荐 Web.Config 源代码网推荐 源代码网推荐 <?xml version="1.0"?> 源代码网推荐<!-- 注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来 源代码网推荐 源代码网推荐 配置应用程序的设置。可以使用 Visual Studio 中的"网站"->"ASP.NET 配置"选项。 源代码网推荐 源代码网推荐 设置和注释的完整列表在machine.config.comments 中,该文件通常位于 WindowsMicrosoft.NETFrameworkv2.0.xxxxxConfig 中 源代码网推荐--> 源代码网推荐<configuration> 源代码网推荐<appSettings/> 源代码网推荐<connectionStrings/> 源代码网推荐<system.web> 源代码网推荐<!-- 源代码网推荐设置 compilation debug="true" 将调试符号插入已编译的页面中。 源代码网推荐但由于这会影响性能,因此只在开发过程中将此值设置为 true。 源代码网推荐--> 源代码网推荐<compilation debug="true"/> 源代码网推荐<!-- 源代码网推荐通过 <authentication> 节可以配置 ASP.NET 使用的 源代码网推荐安全身份验证模式,以标识传入的用户。 源代码网推荐--> 源代码网推荐<authentication mode="Windows"/> 源代码网推荐<!-- 源代码网推荐如果在执行请求的过程中出现未处理的错误,则通过 <customErrors> 节 源代码网推荐可以配置相应的处理步骤。具体说来,开发人员通过该节可以 源代码网推荐配置要显示的 html 错误页以代替错误堆栈跟踪。 源代码网推荐--> 源代码网推荐<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 源代码网推荐<!-- 源代码网推荐<error statusCode="403" redirect="NoAccess.htm" /> 源代码网推荐<error statusCode="404" redirect="FileNotFound.htm" /> 源代码网推荐--> 源代码网推荐</customErrors> 源代码网推荐<!-- 源代码网推荐完全限定客户端重定向的 URL 源代码网推荐有些移动设备要求对客户端重定向的 URL 进行完全限定。 源代码网推荐--> 源代码网推荐<httpRuntime useFullyQualifiedRedirectUrl="true"/> 源代码网推荐<!-- 源代码网推荐指定无 Cookie 的数据字典类型 源代码网推荐这将使字典内容出现在本地请求 url 查询字符串中。 源代码网推荐这是在无 Cookie 的设备上进行 Forms 身份验证所必需的。 源代码网推荐--> 源代码网推荐<mobileControls cookielessDataDictionaryType="System.Web.Mobile.CookielessData"/> 源代码网推荐 源代码网推荐<sessionState mode="InProc" cookieless="true" timeout="20"></sessionState> 源代码网推荐 源代码网推荐<deviceFilters> 源代码网推荐<filter name="isJPhone" compare="Type" argument="J-Phone"/> 源代码网推荐<filter name="isHTML32" compare="PreferredRenderingType" argument="html32"/> 源代码网推荐<filter name="isWML11" compare="PreferredRenderingType" argument="wml11"/> 源代码网推荐<filter name="isCHTML10" compare="PreferredRenderingType" argument="chtml10"/> 源代码网推荐<filter name="isGoAmerica" compare="Browser" argument="Go.Web"/> 源代码网推荐<filter name="isMME" compare="Browser" argument="Microsoft Mobile Explorer"/> 源代码网推荐<filter name="isMyPalm" compare="Browser" argument="MyPalm"/> 源代码网推荐<filter name="isPocketIE" compare="Browser" argument="Pocket IE"/> 源代码网推荐<filter name="isUP3x" compare="Type" argument="Phone.com 3.x Browser"/> 源代码网推荐<filter name="isUP4x" compare="Type" argument="Phone.com 4.x Browser"/> 源代码网推荐<filter name="isEricssonR380" compare="Type" argument="Ericsson R380"/> 源代码网推荐<filter name="isNokia7110" compare="Type" argument="Nokia 7110"/> 源代码网推荐<filter name="prefersGIF" compare="PreferredImageMIME" argument="image/gif"/> 源代码网推荐<filter name="prefersWBMP" compare="PreferredImageMIME" argument="image/vnd.wap.wbmp"/> 源代码网推荐<filter name="supportsColor" compare="IsColor" argument="true"/> 源代码网推荐<filter name="supportsCookies" compare="Cookies" argument="true"/> 源代码网推荐<filter name="supportsJavaScript" compare="Javascript" argument="true"/> 源代码网推荐<filter name="supportsVoiceCalls" compare="CanInitiateVoiceCall" argument="true"/> 源代码网推荐</deviceFilters> 源代码网推荐</system.web> 源代码网推荐</configuration> 源代码网推荐 下面的是Pocket IE和Openwave浏览器在运行该程序时的截图: 源代码网推荐 源代码网推荐
识别Selection列表控件中被选中的数据项(多项模式) <%@ 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"> 源代码网推荐<Item Text="Dopoda" Value="P800" /> 源代码网推荐<Item Text="Motorola" Value="A1200" /> 源代码网推荐<Item Text="Nokia" Value="N70" /> 源代码网推荐<Item Text="Samsung" Value="E638" /> 源代码网推荐</mobile:SelectionList> 源代码网推荐<mobile:Command ID="Command1" Runat="server" OnClick="HandleMultiTeamSelection">提交选择</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> 源代码网推荐 程序代码说明:由于我们要在这个列表控件中实现多选,为此将Selection列表控件的SelectType设置为MultiSelectListBox。而后在Form2控件中添加一个TextView控件,使得所有被选中的数据项的字符信息和隐藏值信息都可以显示出来。 源代码网推荐 源代码网推荐 Default.aspx.cs: 源代码网推荐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 HandleMultiTeamSelection(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; 源代码网推荐} 源代码网推荐} 源代码网推荐 下面的对应的截图: 源代码网推荐 源代码网推荐
绑定数据源 <%@ 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> 源代码网推荐 源代码网推荐Default.aspx.cs: 源代码网推荐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; 源代码网推荐} 源代码网推荐} 源代码网推荐 源代码网推荐 Mobile.cs: 源代码网推荐 源代码网推荐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; } } 源代码网推荐} 源代码网推荐 源代码网推荐
源代码网供稿. |

















