建立使用者介面項目,可讓使用者叫用 ASP.NET 事件處理常式,並且提供將使用者輸入從 UI 項目回傳至伺服器的方式。
命名空間: System.Web.UI.MobileControls
組件: System.Web.Mobile (在 system.web.mobile.dll 中)
語法
'宣告
Public Class Command
Inherits TextControl
Implements IPostBackEventHandler, IPostBackDataHandler
'用途
Dim instance As Command
public class Command : TextControl, IPostBackEventHandler, IPostBackDataHandler
public ref class Command : public TextControl, IPostBackEventHandler, IPostBackDataHandler
public class Command extends TextControl implements IPostBackEventHandler, IPostBackDataHandler
public class Command extends TextControl implements IPostBackEventHandler, IPostBackDataHandler
備註
Command 控制項會在要求的裝置上顯示為互動 UI 項目。UI 項目的標籤來自 Text 屬性,其繼承自 TextControl 基底類別。
警告
請避免在 ASP.NET Mobile Web 網頁 URL 中使用特殊字元。為了將 Command 事件回傳至伺服器而產生的 HREF 標記並未經過嚴格驗證。例如,包含空格的 URL 會導致產生某些 WML 瀏覽器無法處理的 WML。
| 主題 | 位置 |
|---|---|
| 逐步解說:建立適用於行動裝置的 Web 網頁 | 在 Visual Studio 中建置 ASP .NET Web 應用程式 |
範例
在下列程式碼中,示範了如何附加命令事件。按一下任何 Command 按鈕都會引發 OnItemCommand 事件。使用者定義函式使用 CommandEventArgs 引數,查看按下了哪個 Command 按鈕。
注意事項 |
|---|
在下列程式碼中,使用了單一檔案程式碼模型,如果直接複製至程式碼後置 (Code-Behind) 的檔案則可能無法作用。這個程式碼範例必須複製到副檔名為 .aspx 的空白文字檔。如需詳細資訊,請參閱 ASP.NET Web 網頁程式碼模型。 |
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<%@ Import Namespace="System.Web.Mobile" %>
<script runat="server">
Public Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
Dim caps As System.Web.Mobile.MobileCapabilities _
= CType(Request.Browser, MobileCapabilities)
If caps.MaximumSoftkeyLabelLength = 5 Then
Command1.SoftkeyLabel = "Click"
ElseIf caps.MaximumSoftkeyLabelLength > 5 Then
Command1.SoftkeyLabel = "Submit"
End If
End Sub
Private Sub Command_Click(ByVal sender As Object, _
ByVal e As CommandEventArgs)
Dim txt As String = "You clicked Button{0}. ({1} points)"
If e.CommandName.ToString() = "Command1" Then
Label1.Text = String.Format(txt, 1, e.CommandArgument)
ElseIf e.CommandName.ToString() = "Command2" Then
Label1.Text = String.Format(txt, 2, e.CommandArgument)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="form1" runat="server">
<mobile:Label id="Label1" runat="server">
Click a button
</mobile:Label>
<mobile:Label id="Label2" runat="server" />
<mobile:Command id="Command1" Format="Button"
OnItemCommand="Command_Click"
CommandName="Command1" runat="server"
Text="Button1" CommandArgument="70" />
<mobile:Command id="Command2" Format="Link"
OnItemCommand="Command_Click"
CommandName="Command2" runat="server"
Text="Button2" CommandArgument="50" />
</mobile:form>
</body>
</html>
<%@ Page Language="C#"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<%@ Import Namespace="System.Web.Mobile" %>
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
MobileCapabilities caps
= (MobileCapabilities)Request.Browser;
if (caps.MaximumSoftkeyLabelLength == 5)
{
Command1.SoftkeyLabel = "Click";
}
else if (caps.MaximumSoftkeyLabelLength > 5)
{
Command1.SoftkeyLabel = "Submit";
}
}
void Command_Click(object sender, CommandEventArgs e)
{
string txt = "You clicked Button{0}. ({1} points)";
if (e.CommandName.ToString() == "Command1")
{
Label1.Text = String.Format(txt, 1,
e.CommandArgument);
}
else if (e.CommandName.ToString() == "Command2")
{
Label1.Text = String.Format(txt, 2,
e.CommandArgument);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="form1" runat="server">
<mobile:Label id="Label1" runat="server">
Click a button
</mobile:Label>
<mobile:Label id="Label2" runat="server" />
<mobile:Command id="Command1" Format="Button"
OnItemCommand="Command_Click"
CommandName="Command1" runat="server"
Text="Button1" CommandArgument="70" />
<mobile:Command id="Command2" Format="Link"
OnItemCommand="Command_Click"
CommandName="Command2" runat="server"
Text="Button2" CommandArgument="50" />
</mobile:form>
</body>
</html>
.NET Framework 安全性
- AspNetHostingPermission 用於裝載環境中的作業。要求值:LinkDemand,權限值:Minimal。
- AspNetHostingPermission 用於裝載環境中的作業。要求值:InheritanceDemand,權限值:Minimal。
繼承階層架構
System.Object
System.Web.UI.Control
System.Web.UI.MobileControls.MobileControl
System.Web.UI.MobileControls.TextControl
System.Web.UI.MobileControls.Command
執行緒安全
這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。
平台
Windows 98、 Windows 2000 SP4、 Windows Millennium Edition、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1
請參閱
參考
Command 成員
System.Web.UI.MobileControls 命名空間
TextControl
注意事項