Freigeben über


How to: Create Event Handlers in ASP.NET Web Pages

You can create event handlers for ASP.NET Web pages or Web server controls by creating an event-handling method and then binding the page or control to the method declaratively. There are different procedures for creating event handlers:

  • For page events.

  • For controls.

  • For controls if you are using Visual Basic.

NoteNote

You can also add events for client script, which execute when the control is running in the browser. For details, see How to: Add Client Script Events to ASP.NET Web Server Controls.

To create a handler for page events

  • In the page code, create a method with the name **Page_**event. For example, to create a handler for the page's Load event, create a method named Page_Load.

    NoteNote

    Page event handlers are not required to take parameters as other event-handling methods do.

    ASP.NET pages automatically bind page events to methods that have the name **Page_**event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is true by default. If you set AutoEventWireup to false, the page does not automatically look for methods that use the **Page_**event naming convention.

    The following code example shows a handler for the page's Load event. It requires a Label control named postbackMsg to display the text.

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Page.IsPostBack Then
            postbackMsg.Text = "<br />Page has been posted back."
        End If
    End Sub
    
    void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
            postbackMst.Text = "<br />Page has been posted back.";
        }
    }
    

To add handlers declaratively in controls

  1. In the page code, create an event handler with the appropriate signature. The handler can have any name you like.

  2. In the control's markup, add the event name with the on prefix and the handler name as the value.

    The following code example shows the markup for a TextBox control whose TextChanged event is bound to the method named NameChange:

    Security noteSecurity Note

    A TextBox 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.

    <asp:TextBox ID="textbox1" 
        Runat="server" 
        OnTextChanged="NameChange" 
        Text="" />
    

To add handlers implicitly (Visual Basic only)

  1. In the page code, create an event handler with the appropriate signature. The handler can have any name you like.

  2. Add the Handles keyword to the method declaration, specifying the control and event to bind the method to.

    The following code example shows an event handler for the Click event of a button named SampleButton. The method is named Clicked, but could be named anything.

    Sub Clicked(ByVal sender As Object, ByVal e As EventArgs) _
             Handles SampleButton.Click
        ' Code here.
    End Sub
    
    NoteNote

    If you use the Handles keyword, you cannot also include an event attribute in the control markup. If you do, the handler will be called twice.

  3. If the method handles multiple events, add the names of the additional events to the Handles clause, separated by a comma.

    The following code example shows a method that handles the Click event for several buttons. In the handler, the code tests the sender argument to determine which button was clicked.

    Sub Clicked(ByVal sender As Object, ByVal e As EventArgs) _
             Handles Button1.Click, Button2.Click, Button3.Click
        Dim b As Button = CType(sender, Button)
        Response.Write("You clicked the button labeled " & b.Text
    End Sub
    

See Also

Tasks

How to: Dynamically Bind Event Handlers at Run Time in ASP.NET Web Pages

Other Resources

Server Event Handling in ASP.NET Web Pages
ASP.NET Web Server Controls
Global.asax Syntax