Command 控件提供了一种从用户界面元素调用 ASP.NET 事件处理程序的方法,从而可以将用户输入从用户界面元素回发到服务器。Command 控件实现 IPostBackDataHandler 接口,因此它可以处理通过单击图像创建的回发事件。
移动控件语法
必需的属性、默认值和具有代码特征的元素以粗体显示。
<mobile:Command
runat="server"
id="id"
Font-Name="fontName"
Font-Size="{NotSet|Normal|Small|Large}"
Font-Bold="{NotSet|False|True}"
Font-Italic="{NotSet|False|True}"
ForeColor="foregroundColor"
BackColor="backgroundColor"
Alignment="{NotSet|Left|Center|Right}"
BreakAfter="{True|False}"
EnableViewState="{True|False}"
StyleReference="styleReference"
Text="text"
Visible="{True|False}"
Wrapping="{NotSet|Wrap|NoWrap}"
CausesValidation="{NotSet|False|True}"
CommandArgument="commandArgument"
CommandName="commandName"
Format="Button|Link"
ImageUrl="imageUrl"
OnClick="clickEventHandler"
OnItemCommand="commandEventHandler"
SoftkeyLabel="softkeyLabel">
innerText
</mobile:Command>
包容规则
以下控件可以包含 Command 控件。
控件 说明
System.Web.UI.MobileControls.Form 可以包含任意多个 Command 控件。
System.Web.UI.MobileControls.Panel 可以包含任意多个 Command 控件。
文本控件不能包含任何其他控件。
设备模板
无。
特定于设备的行为
呈现命令的样式将根据设备的不同有所不同,但命令的文本将显示在所有设备上。您可以从所有设备调用 Command 控件。
设备语言 行为描述
HTML Command 控件作为按钮呈现为一个 <input> 标记,后接一个 <br/> 标记
如果同一个窗体上有多个命令并发生一个提交操作,则第一个命令将接收 Click 事件。但是,在某些 HTML 窗体上,ENTER 键无效。如果在具有 Command 控件的页上按 ENTER 键,则第一个命令将接收 Click 事件。
WML Command 控件以下面的格式生成 WML。
<anchor>TextProperty<go href="postbackinfo"/> </go></anchor>
<br/>
注意 您必须避免在 ASP.NET 移动 Web 窗体页 URL 中使用特殊字符。为将命令事件回发到服务器而生成的 HREF 标记未经严格验证。包含空格的 URL 导致所生成的 WML 不能被某些 WML 浏览器处理。
在某些情况下(例如窗体提交),Command 控件以 <do> 元素而不是定位点的形式呈现给 WML。在这些情况下,将忽略 FontSize、FontStyle 和 Wrapping 属性。
示例
下面的示例使用两个标签,每个窗体上使用一个。第一个标签未进行修改,因此不需要 ID 值。第二个标签的 ID 值是 WelcomeMessage,在 Button_OnClick 事件处理程序中设置其文本值。
< %@ Page Inherits =
" System.Web.UI.MobileControls.MobilePage " Language = " VB " % >
< %@ Register TagPrefix = " mobile "
Namespace = Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script language="vb" runat="server">
protected Sub Button_OnClick()Sub Button_OnClick(sender As Object, e As EventArgs)
WelcomeMessage.Text = "Welcome to my app, " + NameEdit.Text
ActiveForm = SecondPanel
End Sub
</script>
<mobile:Form runat="server">
<mobile:Label runat="server">Enter your name</mobile:Label>
<mobile:TextBox runat="server" id="NameEdit" />
<mobile:Command runat="server" id="Button" OnClick="Button_OnClick">
OK
</mobile:Command>
</mobile:Form>
<mobile:Form runat="server" id="SecondPanel">
<mobile:Label runat="server" id="WelcomeMessage" />
</mobile:Form>
<% @ Page Inherits =
" System.Web.UI.MobileControls.MobilePage " Language = " C# " %>
<% @ Register TagPrefix = " mobile "
Namespace = " System.Web.UI.MobileControls "
Assembly = " System.Web.Mobile " %>
< script language = " c# " runat = " server " >
protected void Button_OnClick(Object sender, EventArgs e)
... {
WelcomeMessage.Text = "Welcome to my app, " + NameEdit.Text;
ActiveForm = SecondPanel;
}
</ script >
< mobile:Form runat = " server " >
< mobile:Label runat = " server " > Enter your name </ mobile:Label >
< mobile:TextBox runat = " server " id = " NameEdit " />
< mobile:Command runat = " server " id = " Button " OnClick = " Button_OnClick " >
OK
</ mobile:Command >
</ mobile:Form >
< mobile:Form runat = " server " id = " SecondPanel " >
< mobile:Label runat = " server " id = " WelcomeMessage " />
</ mobile:Form >