更新:2007 年 11 月
选择 SelectionList ASP.NET 移动控件中的项并不会从服务器生成响应。必须将包含 SelectionList 控件的窗体发回到服务器。这通常用 Command 控件来完成。当 Command 控件将窗体发回到服务器时,SelectionList 控件会引发 SelectedIndexChanged 事件。应用程序可提供处理此事件的方法。
响应选择的另一种方法是添加对可以处理客户端 JavaScript(如 HTML 浏览器)的设备的支持。对于这些设备,请使用以下过程:
添加一个 <DeviceSpecific> 元素,其 <Choice> 筛选器设置为“supportsJavaScript”。
在包含 ASP.NET DropDownList 控件的选项中创建一个内容模板。它是非移动 ASP.NET 服务器控件。
将 DropDownList 控件的 AutoPostBack属性设置为 true。
您必须用内容模板为其他所有不支持 JavaScript 和使用 SelectionList 控件的设备创建一个 <DeviceSpecific> 筛选器。
下面的代码示例演示此项技术:
<mobile:Panel id="Panel1" runat="server">
<mobile:DeviceSpecific id="DeviceSpecific1" runat="server">
<Choice Filter="supportsJavaScript">
<ContentTemplate>
<asp:DropDownList id="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="a">1</asp:ListItem>
<asp:ListItem Value="b">2</asp:ListItem>
<asp:ListItem Value="c">3</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</Choice>
<Choice>
<ContentTemplate>
<mobile:SelectionList id="SelectionList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<Item Value="a" Text="1"/>
<Item Value="a" Text="2"/>
<Item Value="a" Text="3"/>
</mobile:SelectionList>
<mobile:Command runat="server" text="Submit"/>
</ContentTemplate>
</Choice>
</mobile:DeviceSpecific>
</mobile:Panel>
该示例需要 Web.config 文件中的下列设置:
<configuration>
<system.web>
<deviceFilters>
<filter name="supportsJavaScript"
compare="javascript"
argument="true"/>
</deviceFilters>
</system.web>
</configuration>
在跨页发送时处理请求差异
SelectionList 控件可以在跨页发送的特殊情况下要求附加的处理。例如,考虑 Source.aspx 和 Destination.aspx 这两个页,其中 Source.aspx 包含 SelectionList 控件并发送到 Destination.aspx。
下面的示例演示 Source.aspx 页的标记:
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage"
Language="C#" %>
<mobile:Form runat="server" method="post" action="destination.aspx">
<mobile:SelectionList runat="server"
selectType="MultiSelectListBox" id="MultiSelectList1">
<item text="I" value="1" />
<item text="ii" value="2" />
<item text="iii" value="3" />
</mobile:SelectionList>
<mobile:command runat=server text="Post" />
</mobile:Form>
下面的示例演示 Destination.aspx 页的标记和代码:
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage"
Language="C#" %>
<%@ Register TagPrefix="Mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat=server language=cs>
public void Page_Load()
{
Label1.Text = Request["MultiSelectList1"];
}
</script>
<mobile:form runat=server>
<mobile:label id="Label1" runat="server" />
</mobile:form>
假设用户浏览到 Source.aspx,选择列表框中的第一和第三项,并单击命令按钮,这将导致该页被发送到 Destination.aspx。为 Destination.aspx 中的 Label1 显示的文本将根据标记语言和设备的不同而变化。这些差异是由于浏览器实现中的 HTML 和 WML 规格和差异造成的。您可能会看到以下结果:
目标 |
结果 |
说明 |
---|---|---|
HTML 浏览器 |
"1, 3" |
由逗号和空格分隔,不带尾随分隔符。 |
WML 设备 1 |
"1;3" |
由分号分隔,不带尾随分隔符。 |
WML 设备 2 |
"1;3;" |
由分号分隔,带尾随分隔符。 |
为更方便地使用目标页中 Request["MultiSelectList1"] 变量的值,您可以按照下面的示例所示对选择列表发出的数据进行预处理。这将使各种可能的情况实现标准化,以符合 HTML 浏览器使用的格式。
public void Page_Load()
{
String selections = Request["MultiSelectList1"];
if (selections.Length > 0 &&
selections [selections.Length - 1] == ';')
{
selections = selections.Substring(0, selections.Length - 1);
}
Label1.Text = selections.Replace(";", ", ");
}
![]() |
---|
在回发到包含 SelectionList 控件的同一页这一常见的情况下,该特殊处理是不必要的。 |
某些 cHTML 设备要求每个复选框具有唯一的名称。在这种情况下,对于每个复选框,所生成的复选框名称都将采用 identifier*item number 的形式。当编写跨页方案的代码时,可以使用类似于下面的示例的代码。在该示例中,MyPage1.aspx 包含以下移动 Web 窗体和 SelectionList 控件。
<mobile:form runat=server action=MyPage2.aspx>
<mobile:selectionList runat="server" id="mySList ...>
...
</mobile:form>
MyPage2.aspx 具有以下代码:
<script runat="server">
// Create a Form just for the list selections
System.Collections.Specialized.NameValueCollection _myForm =
new NameValueCollection();
public void Page_Init()
{
// Process the Form
foreach(String key in Request.Form.Keys)
{
// Look for an asterisk in the key
int pos = key.LastIndexOf('*');
if (pos > -1)
{
// Add the modified key to the Form
_myForm.Add(key.Substring(0, pos), Request.Form[key])
}
Else
{
// Or add the unmodified key to the Form
_myForm.Add(key, Request.Form[key]);
}
}
}
// Use _myForm in place of Request.Form
public void Page_Load()
{
// Get the processed list of selected items
String selectedValues = _myForm["mySList"];
// etc.
}
</script>