ImageButton.OnCommand(CommandEventArgs) Metodo

Definizione

Genera l'evento Command e consente la gestione diretta dell'evento 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)

Parametri

e
CommandEventArgs

Oggetto CommandEventArgs che contiene i dati dell'evento.

Esempio

Nell'esempio seguente viene illustrato come specificare e codice un gestore per l'evento Command per determinare quale ImageButton controllo viene fatto clic.

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. Questo esempio di codice deve essere copiato in un file di testo vuoto con estensione aspx. Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.

<%@ 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>

Commenti

L'evento Command viene generato quando viene fatto clic sul ImageButton controllo. Il OnCommand gestore eventi viene usato per fare in modo che il ImageButton controllo si comporta come un pulsante Command. Un nome di comando può essere associato al controllo usando la CommandName proprietà . Ciò consente l'immissione di più ImageButton controlli nella pagina Web. Il valore in questa proprietà può quindi essere identificato a livello di codice nel OnCommand gestore eventi per determinare l'azione appropriata da eseguire quando ogni ImageButton controllo viene fatto clic. La CommandArgument proprietà può essere usata anche per passare informazioni aggiuntive sul comando, ad esempio specificando l'ordine crescente.

Nota

L'evento Command viene generato tramite la gerarchia di controllo sotto forma di BubbleEvent.

Quando viene generato un evento, il gestore dell'evento viene richiamato tramite un delegato. Per altre informazioni, vedere Procedura: Usare eventi in un'applicazione Web Forms.

Il metodo OnCommand consente inoltre alle classi derivate di gestire l'evento senza associare un delegato. È la tecnica consigliata per la gestione dell'evento in una classe derivata.

Note per gli eredi

Quando si esegue l'override di OnCommand(CommandEventArgs) in una classe derivata, verificare di chiamare il metodo OnCommand(CommandEventArgs) della classe di base in modo che i delegati registrati ricevano l'evento.

Si applica a

Vedi anche