Share via


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) 메서드를 호출해야 합니다.

적용 대상

추가 정보