ImageButton.CommandName 屬性

定義

取得或設定與 ImageButton 控制項相關的命令名稱。

public:
 property System::String ^ CommandName { System::String ^ get(); void set(System::String ^ value); };
public string CommandName { get; set; }
[System.Web.UI.Themeable(false)]
public string CommandName { get; set; }
member this.CommandName : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.CommandName : string with get, set
Public Property CommandName As String

屬性值

ImageButton 控制項相關的命令名稱。 預設值是 Empty

實作

屬性

範例

下列範例示範如何使用 CommandName 屬性,將命令名稱與 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")
            Label1.Text = "You clicked the Sort Button";
         else
            Label1.Text = "You clicked the Edit 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"
           ImageUrl="images/pict1.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"/>

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

      <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" Then
            Label1.Text = "You clicked the Sort Button"
         Else
            Label1.Text = "You clicked the Edit 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"
           ImageUrl="images/pict1.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"/>

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

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

   </form>

</body>
</html>

備註

使用這個屬性可指定要在按一下控制項時 ImageButton 執行的命令,例如 SortCancelEdit 。 這可讓多個 ImageButton 控制項放在相同的網頁上。 此屬性中的值接著可以在事件處理常式中 OnCommand 以程式設計方式識別,以判斷按一下每個 ImageButton 控制項時要執行的適當動作。

您可以使用 屬性來 CommandArgument 包含命令的補充資訊,例如指定遞增排序次序。

這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttributeASP.NET 主題和外觀

適用於

另請參閱