SelectionList 控制項和回傳
更新:2007 年 11 月
選取 SelectionList ASP.NET Mobile 控制項中的項目並不會造成伺服器產生回應。其中出現 SelectionList 控制項的表單必須回傳至伺服器。而這個動作通常是使用 Command 控制項完成。當 Command 控制項將表單回傳到伺服器時,SelectionList 控制項便會引發 SelectedIndexChanged 事件。您的應用程式可提供處理這個事件的方法。
回應選取的另一種方式,就是加入可處理用戶端 JavaScript 之裝置 (例如,HTML 瀏覽器) 的支援。針對這些裝置,請使用下列程序:
加入 <Choice> 篩選條件已設為 "supportsJavaScript" 的 <DeviceSpecific> 項目。
在 <Choice> 中建立包含 ASP.NET DropDownList 控制項之內容樣板。這是非行動 ASP.NET 伺服器控制項。
將 DropDownList 控制項的 AutoPostBack屬性設定為 true。
您必須針對所有不支援 JavaScript 並使用 SelectionList 控制項的其他裝置,使用內容樣板來建立 <DeviceSpecific> 篩選條件。
下列程式碼範例會示範這項技巧:
<mobile:Panel id="Panel1" >
<mobile:DeviceSpecific id="DeviceSpecific1" >
<Choice Filter="supportsJavaScript">
<ContentTemplate>
<asp:DropDownList id="DropDownList1"
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"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<Item Value="a" Text="1"/>
<Item Value="a" Text="2"/>
<Item Value="a" Text="3"/>
</mobile:SelectionList>
<mobile:Command 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 method="post" action="destination.aspx">
<mobile:SelectionList
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" />
</mobile:form>
假設使用者瀏覽至 Source.aspx,選取了清單方塊中的第一個和第三個項目,接著按一下命令按鈕,使得該網頁回傳到 Destination.aspx。顯示於 Destination.aspx 中的 Label1 文字,將會根據標記語言和裝置而有所不同。這些差異起因於 HTML 與 WML 規格及瀏覽器實作 (Implementation) 差異。您可能會看到下列結果:
目標 |
結果 |
說明 |
---|---|---|
HTML 瀏覽器 |
"1, 3" |
以不含後端分隔符號 (Delimiter) 的逗號和空白分隔 |
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 包含下列 Mobile Web Form 和 SelectionList 控制項。
<mobile:form runat=server action=MyPage2.aspx>
<mobile:selectionList id="mySList ...>
...
</mobile:form>
MyPage2.aspx 包含下列程式碼:
<script >
// 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>