ImageButton.OnCommand(CommandEventArgs) 方法

定义

引发 Command 事件并允许直接处理 Command 事件。

protected:
 virtual void OnCommand(System::Web::UI::WebControls::CommandEventArgs ^ e);
protected virtual void OnCommand (System.Web.UI.WebControls.CommandEventArgs e);
abstract member OnCommand : System.Web.UI.WebControls.CommandEventArgs -> unit
override this.OnCommand : System.Web.UI.WebControls.CommandEventArgs -> unit
Protected Overridable Sub OnCommand (e As CommandEventArgs)

参数

e
CommandEventArgs

包含事件数据的 CommandEventArgs

示例

以下示例演示如何为 事件指定和编码处理程序 Command ,以确定单击哪个 ImageButton 控件。

注意

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ImageButton CommandName Sample</title>
<script language="C#" runat="server">

      void ImageButton_Command(object sender, CommandEventArgs e) 
      {
         if (e.CommandName == "Sort" && e.CommandArgument == "Ascending")
            Label1.Text = "You clicked the Sort Ascending Button";
         else
            Label1.Text = "You clicked the Sort Descending Button";
      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3>ImageButton CommandName Sample</h3>

      Click an image.<br /><br />

      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="Sort Ascending"
           ImageUrl="images/pict.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Ascending"/>

      <asp:ImageButton id="imagebutton2" runat="server"
           AlternateText="Sort Descending"
           ImageUrl="image/pict2.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Descending"/>

      <br /><br />
    
      <asp:label id="Label1" runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ImageButton CommandName Sample</title>
<script language="VB" runat="server">

      Sub ImageButton_Command(sender As Object, e As CommandEventArgs) 
         If (e.CommandName = "Sort") And (e.CommandArgument = "Ascending") Then
            Label1.Text = "You clicked the Sort Ascending Button"
         Else
            Label1.Text = "You clicked the Sort Descending Button"
         End If
      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3>ImageButton CommandName Sample</h3>

      Click an image.<br /><br />

      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="Sort Ascending"
           ImageUrl="images/pict.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Ascending"/>

      <asp:ImageButton id="imagebutton2" runat="server"
           AlternateText="Sort Descending"
           ImageUrl="images/pict2.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Descending"/>

      <br /><br />
    
      <asp:label id="Label1" runat="server"/>

   </form>

</body>
</html>

注解

单击 Command 控件时将 ImageButton 引发 该事件。 事件处理程序 OnCommand 用于使 ImageButton 控件的行为类似于命令按钮。 可以使用 属性将命令名称与 控件 CommandName 相关联。 这允许在网页上放置多个 ImageButton 控件。 然后,可以在事件处理程序中 OnCommand 以编程方式标识此属性中的值,以确定单击每个 ImageButton 控件时要执行的相应操作。 属性 CommandArgument 还可用于传递有关命令的其他信息,例如指定升序。

注意

事件 Command 以 的形式 BubbleEvent通过控件层次结构引发。

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅如何:在Web Forms应用程序中使用事件

OnCommand 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnCommand(CommandEventArgs) 时,一定要调用基类的 OnCommand(CommandEventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅