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 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")
            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 主题和皮肤

适用于

另请参阅