JavaScript Editor jscript editor     Web designer 



Main Page

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:

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.

    Visual BasicВ CopyCode imageCopy Code
    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

    C#В CopyCode imageCopy Code
    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:

    В CopyCode imageCopy Code
    <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.

    Visual BasicВ CopyCode imageCopy Code
    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.

    Visual BasicВ CopyCode imageCopy Code
    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



JavaScript Editor jscript editor     Web designer