LinkButton.OnCommand(CommandEventArgs) Método

Definición

Provoca el evento Command del control LinkButton.

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)

Parámetros

e
CommandEventArgs

Objeto CommandEventArgs que contiene los datos del evento.

Ejemplos

En el ejemplo siguiente se muestra cómo especificar y codificar un controlador para que el Command evento muestre qué LinkButton control se hace clic.

<%@ 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>LinkButton Command Event Example</title>
<script language="C#" runat="server">
     
      void LinkButton_Command(Object sender, CommandEventArgs e) 
      {
         Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
      }
 
   </script>
 
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>LinkButton Command Event Example</h3>
  
      <asp:LinkButton id="LinkButton1" 
           Text="Order Item 10001"
           CommandName="Order" 
           CommandArgument="10001" 
           OnCommand="LinkButton_Command" 
           runat="server"/>
 
      <br />
  
      <asp:LinkButton id="LinkButton2" 
           Text="Order Item 10002"
           CommandName="Order" 
           CommandArgument="10002" 
           OnCommand="LinkButton_Command" 
           Runat="server"/>
 
      <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>LinkButton Command Event Example</title>
<script language="VB" runat="server">
     
      Sub LinkButton_Command(sender As Object, e As CommandEventArgs) 
         Label1.Text = "You chose: " & e.CommandName & " Item " & _
            e.CommandArgument
      End Sub
 
   </script>
 
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>LinkButton Command Event Example</h3>
  
      <asp:LinkButton id="LinkButton1" 
           Text="Order Item 10001"
           CommandName="Order" 
           CommandArgument="10001" 
           OnCommand="LinkButton_Command" 
           runat="server"/>
 
      <br />
  
      <asp:LinkButton id="LinkButton2" 
           Text="Order Item 10002"
           CommandName="Order" 
           CommandArgument="10002" 
           OnCommand="LinkButton_Command" 
           Runat="server"/>
 
      <br />
      <br />
 
      <asp:Label id="Label1" runat="server"/>
 
   </form>

</body>
</html>

Comentarios

El Command evento se genera cuando se hace clic en el LinkButton control. Este evento se usa normalmente cuando un nombre de comando, como Sort, está asociado al LinkButton control . Esto le permite crear varios LinkButton controles en una página web y determinar mediante programación qué LinkButton control se hace clic.

Nota

El Command evento se genera a través de la jerarquía de controles en forma de BubbleEvent.

Cuando se genera un evento, se invoca el controlador de eventos a través de un delegado. Para obtener más información, consulte controlar y provocar eventos.

El método OnCommand también permite que las clases derivadas controlen el evento sin adjuntar ningún delegado. Ésta es la técnica preferida para controlar el evento en una clase derivada.

Notas a los desarrolladores de herederos

Al reemplazar OnCommand(CommandEventArgs) en una clase derivada, asegúrese de llamar al método OnCommand(CommandEventArgs) de la clase base para que los delegados registrados reciban el evento.

Se aplica a

Consulte también