Partager via


Exemple d'événement de publication

L'exemple suivant crée un bouton personnalisé, MyButton (qui entraîne la publication) capture l'événement de publication et déclenche un événement Click sur le serveur. Pour générer l'exemple, consultez les instructions fournies dans Exemples de contrôles serveur.

using System;
using System.Web.UI;

namespace CustomControls 
{  
   public class MyButton: Control, IPostBackEventHandler 
   {     
      // Defines the Click event.
      public event EventHandler Click;
      
      // Invokes delegates registered with the Click event.
      protected virtual void OnClick(EventArgs e) 
      {     
         if (Click != null) 
         {
            Click(this, e);
         }  
      }
      
      // Method of IPostBackEventHandler that raises change events.
      public void RaisePostBackEvent(string eventArgument)
      {     
         OnClick(EventArgs.Empty);
      }
      
      protected override void Render(HtmlTextWriter output) 
      {     
         output.Write("<INPUT TYPE=submit name=" + this.UniqueID + 
            " Value='Click Me' />"); 
      }
   }    
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web.UI

Namespace CustomControls
   Public Class MyButton
      Inherits Control
      Implements IPostBackEventHandler
      ' Defines the Click event.
      Public Event Click As EventHandler
      
      ' Invokes delegates registered with the Click event.
      Protected Overridable Sub OnClick(e As EventArgs)
         RaiseEvent Click(Me, e)
      End Sub
      
      ' Method of IPostBackEventHandler that raises change events.
      Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
         OnClick(EventArgs.Empty)
      End Sub
      
      Protected Overrides Sub Render(output As HtmlTextWriter)
         output.Write("<INPUT TYPE=submit name=" & Me.UniqueID & _
               " Value='Click Me' />")
      End Sub
   End Class
End Namespace

Utilisation du contrôle sur une page

La page ASP.NET suivante utilise le bouton personnalisé MyButton défini dans l'exemple précédent.

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="C#" runat=server>
              
private void Button_Click(Object sender, EventArgs e) {
   TextBox.BackColor = System.Drawing.Color.Green;
   TextBox.Text = "You clicked the button";
}
                  
</script>
<html>
   <body>                  
      <form  runat=server>                        
      Here is  the custom button.<br>
      <Custom:MyButton Id = "Button"  OnClick = "Button_Click" runat=server/> 
      <br><br>                              
      <asp:TextBox id = "TextBox" Text = "Click the button" Width = "200"  BackColor = "Cyan" runat=server/> 
      <br>                          
      </form>                             
   </body>                    
</html>
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="VB" runat=server>
   Private Sub Button_Click(sender As Object, e As EventArgs)
      TextBox.BackColor = System.Drawing.Color.Green
      TextBox.Text = "You clicked the button"
   End Sub
</script>
<html>
   <body>                  
      <form  runat=server>                        
      Here is  the custom button.<br>
      <Custom:MyButton Id = "Button"  OnClick = "Button_Click" runat=server/> 
      <br><br>                              
      <asp:TextBox id = "TextBox" Text = "Click the button" Width = "200"  BackColor = "Cyan" runat=server/> 
      <br>                          
      </form>                             
   </body>                    
</html>
                              

Voir aussi

Capture d'événements de publication