ImageButton.OnCommand(CommandEventArgs) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
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
Oggetto CommandEventArgs contenente i dati dell'evento.
Esempio
Nell'esempio seguente viene illustrato come specificare e codificare un gestore per l'evento Command per determinare quale ImageButton controllo viene selezionato.
Annotazioni
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 Form, vedere ASP.NET modello di codice della pagina Web Form.
<%@ 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 si fa clic sul ImageButton controllo . Il OnCommand gestore eventi viene usato per fare in modo che il ImageButton controllo si comporti come un pulsante Command. Un nome di comando può essere associato al controllo utilizzando la CommandName proprietà . In questo modo è possibile posizionare più ImageButton controlli nella pagina Web. Il valore di questa proprietà può quindi essere identificato a livello di codice nel OnCommand gestore eventi per determinare l'azione appropriata da eseguire quando si fa clic su ogni ImageButton controllo. La CommandArgument proprietà può essere usata anche per passare informazioni aggiuntive sul comando, ad esempio specificando l'ordine crescente.
Annotazioni
L'evento Command viene generato tramite la gerarchia dei controlli sotto forma di BubbleEvent.
La generazione di un evento richiama il gestore eventi tramite un delegato. Per altre informazioni, vedere Procedura: Utilizzare eventi in un'applicazione Web Form.
Il OnCommand metodo consente anche alle classi derivate di gestire l'evento senza associare un delegato. Questa è la tecnica preferita per gestire l'evento in una classe derivata.
Note per gli eredi
Quando si esegue l'override OnCommand(CommandEventArgs) in una classe derivata, assicurarsi di chiamare il metodo della OnCommand(CommandEventArgs) classe di base in modo che i delegati registrati ricevano l'evento.