Command 类移动

创建一个用户界面元素以使用户能够调用 ASP.NET 事件处理程序,并且它提供一种将用户输入从用户界面元素回发到服务器的方法。

 

Command 控件在请求设备上显示为一个交互式用户界面元素。用户界面元素的标签来自 Text 属性,该属性继承自 TextControl 基类。

Caution note警告

避免在 ASP.NET 移动网页 URL 中使用特殊字符。不会严格验证为将 Command 事件回发到服务器而生成的 HREF 标记。例如,包含空格的 URL 会导致生成一些 WML 浏览器不能处理的 WML。

下面的代码示例演示如何附加命令事件。单击任何一个 Command 按钮都会引发 OnItemCommand 事件。用户定义的函数使用 CommandEventArgs 参数来查看单击了哪个 Command 按钮。

 

 

<%@ 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  >
<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>

posted @ 2009-04-27 23:43  minmin8110  阅读(166)  评论(0)    收藏  举报