Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    ImageButton OnCommand nel code-behind

    Salve a tutti,
    sto realizzando un nuovo applicativo con vb\asp .net.

    Ho trovato una cosa strana nella gestione degli eventi di ImageButton... volevo cioè creare un evento comune per l'OnCommand di una serie di imagebutton... seguendo l'esempio microsoft (msdn visual studio) e mettendo il gestore evento nel code-nehind non funziona.
    Aggiungendo invece lo script della procedura comune direttamente nella pagina aspx come script funziona tutto regolarmente (una meraviglia) ma vorrei capire perché non si può mettere il gestore direttamente nel code-behind.
    Qualcuno può illuminarmi?

    ciao

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    veramente a me funziona. Ti mando l'esempio che ho fatto.
    codice:
    
    	<asp:imagebutton id="ImageButton1" runat="server" CommandName="comand1"></asp:imagebutton>
    	<asp:ImageButton id="ImageButton2" runat="server" CommandName="comand2"></asp:ImageButton>
    	<asp:ImageButton id="ImageButton3" runat="server" CommandName="comand3"></asp:ImageButton>
    </P>
    
    
    <asp:Label id="Label2" runat="server"></asp:Label></P>
    e nel codice sottostante
    codice:
        Private Sub ImageButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles ImageButton1.Command, ImageButton2.Command, ImageButton3.Command
            Dim ImageButton As ImageButton = DirectCast(sender, ImageButton)
            Me.Label2.Text = ImageButton.ID & ", " & e.CommandName
        End Sub
    Pietro

  3. #3

    già

    ma è proprio quello che dico, cosa sono quei tre handles di seguito alla dichiarazione della sub... se dichiari la sub nello scipt così:

    <script language="vb" runat="server">
    Private Sub ImageButton_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
    Response.Redirect(e.CommandName & ".aspx")
    End Sub
    </script>

    non metti nessun handles basta settare l'OnCommand sugli ImageButton


    ma a sto punto penso che la quantità di codice prodotta sia la stessa.

    bah...

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Gentile amico, tu hai detto di avere trovato strana la gestione degli eventi nel code-nehind e che, mettendo il gestore evento nel code-nehind non funziona...

    Io ti ho fatto un esempio che, se lo hai provato, avrai visto che funziona perfettamente (l'ho collaudato).
    Comunque, ci sono due modi per trattare gli eventi: il primo te l'ho scritto, il secondo, eccolo:
    Nella pagina .aspx:
    codice:
    		<form id="Form1" method="post" runat="server">
    			
    
    
    				<asp:imagebutton id="ImageButton1" runat="server" CommandName="comand1" OnCommand="ImageButton_OnCommand"></asp:imagebutton>
    				<asp:ImageButton id="ImageButton2" runat="server" CommandName="comand2" OnCommand="ImageButton_OnCommand"></asp:ImageButton>
    				<asp:ImageButton id="ImageButton3" runat="server" CommandName="comand3" OnCommand="ImageButton_OnCommand"></asp:ImageButton>
    			</P>
    			
    
    <asp:Label id="Label2" runat="server"></asp:Label></P>
    		</form>
    Nel code-nehind:
    codice:
        Protected Sub ImageButton_OnCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
            Dim ImageButton As ImageButton = DirectCast(sender, ImageButton)
            Me.Label2.Text = ImageButton.ID & ", " & e.CommandName
        End Sub
    Ciao
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.