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 Form程式碼模型的詳細資訊,請參閱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>

備註

按一下控制項時, ImageButton 就會 Command 引發 事件。 OnCommand事件處理常式可用來讓 ImageButton 控制項的行為如同 [命令] 按鈕。 命令名稱可以使用 屬性來與控制項 CommandName 產生關聯。 這可讓多個 ImageButton 控制項放在網頁上。 此屬性中的值接著可以在事件處理常式中 OnCommand 以程式設計方式識別,以判斷按一下每個 ImageButton 控制項時要執行的適當動作。 屬性 CommandArgument 也可以用來傳遞命令的其他資訊,例如指定遞增順序。

注意

事件 Command 會以 的形式透過控制項階層引發 BubbleEvent

引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱如何:在Web Form應用程式中取用事件

OnCommand 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。

給繼承者的注意事項

當在衍生類別中覆寫 OnCommand(CommandEventArgs) 時,請確定呼叫基底類別的 OnCommand(CommandEventArgs) 方法,使已註冊的委派能接收到事件。

適用於

另請參閱