Postback Event Sample

The following sample creates a custom button, MyButton, that causes postback, captures the postback event, and raises a Click event on the server. To build the sample, see the instructions in Server Control Samples.

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

Using the Control on a Page

The following ASP.NET page uses the custom button MyButton, defined in the preceding sample.

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

See Also

Capturing Postback Events