How to: Consume Events in a Web Forms Application

A common scenario in Web Forms applications is to populate a Web page with controls, and then perform a specific action based on which control the user clicks. For example, a System.Web.UI.WebControls.Button control raises an event when the user clicks on it in the Web page. By handling the event, your application can perform the appropriate application logic for that button click.

For information about the Web Forms programming model, see Programming Web Forms.

To handle a button click event on a Web page

  1. Create a Web Forms page (ASP.NET page) that has a Button control.

    <asp:Button id = "Button" Text = "Click Me" runat = server/>
    
  2. Define an event handler that matches the Click event delegate signature. The Click event uses the EventHandler class for the delegate type and the EventArgs class for the event data.

  3. Set the OnClick attribute in the Button element to the event handler method.

    <asp:Button
            id = "Button" OnClick = "Button_Click" Text = "Click Me" 
            runat = server/>
    

    Note

    A Web Forms application developer can wire the event declaratively as shown without directly working with the delegate. The ASP.NET page framework generates code that creates an instance of EventHandler that references Button_Click and adds this delegate instance to the Click event of the Button instance.

Example

The following Web Forms page handles the Click event of Button to change the background color of TextBox.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<html>
   <script language="VB" runat=server> 
      Private Sub Button_Click(sender As Object, e As EventArgs)
         Box.BackColor = System.Drawing.Color.LightGreen
      End Sub
   </script>
   <body> 
      <form method="POST" action="Events.aspx" runat=server>   
          Click the button, and notice the color of the text box.<br><br>
         <asp:TextBox 
         id = "Box" Text = "Hello" BackColor = "Cyan" runat=server/>             
         <br><br>       
        <asp:Button
        id = "Button" OnClick = "Button_Click" Text = "Click Me" 
        runat = server/>         
      </form>
   </body>
</html>
<html>
   <script language="C#" runat=server> 
      private void Button_Click(object sender, EventArgs e){
         Box.BackColor = System.Drawing.Color.LightGreen;
               }
   </script>
   <body> 
      <form method="POST" action="Events.aspx" runat=server>   
          Click the button, and notice the color of the text box.<br><br>
         <asp:TextBox 
         id = "Box" Text = "Hello" BackColor = "Cyan" runat=server/>             
         <br><br>       
        <asp:Button
        id = "Button" OnClick = "Button_Click" Text = "Click Me" 
        runat = server/>         
      </form>
   </body>
</html>

Compiling the Code

To see how event handling works in Web Forms, save the example page to a file with an .aspx extension (which indicates that the file is an ASP.NET page) and deploy it anywhere in your IIS virtual root directory.

See Also

Concepts

Events and Delegates

Consuming Events

Raising an Event

Other Resources

Handling and Raising Events