Export (0) Print
Expand All

Connecting Multiple Events to a Single Event Handler in Web Forms Pages

Visual Studio .NET 2003

If you already have an event handler, you can bind several control events to it. These multiple events can be from the same control or one event from several different controls (as long as the events all have the same signature). For example, you may want to bind the Click events of several Button Web server controls on a Web Forms page to a single event handler.

For both Visual Basic and Visual C#, you must add specific code to the event-handler method signature in the code-behind file.

To connect multiple events to a single event handler in Visual Basic

  • Modify the Handles clause of a method by adding the names of the events that the method should handle. Separate the event names with commas.

    The following example shows how you can have the method MyMultipleEventHandler bound to events raised by three controls:

    Sub MyMultipleEventHandler (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    
    Note   It is up to you to make sure that the method has the correct signature for the event it is handling.

If you are using Visual C#, you associate multiple control events with a single event handler using the Web Forms Designer.

To connect multiple events to a single event handler in Visual C#

  1. In Design view, select a control whose event you want to bind to a method.
  2. In the Properties window, click the Events button (Aa984308.vbeventsbutton(en-us,VS.71).gif).

    The Properties window displays a list of the events for the control, with boxes to the right that display the names of event handlers bound to those events.

  3. From the drop-down list, select the method to bind the event to. The methods displayed in the list are those that have the correct signature for the event you want to handle.

    The Code Editor opens, showing the method you selected. In addition, a line is added to the page's InitializeComponent method to perform the binding. The following shows an example of two button Click events that call the same handler:

    this.Button1.Click += new System.EventHandler(this.myEventHandler);
    this.Button2.Click += new System.EventHandler(this.myEventHandler);
    
  4. Repeat Steps 1 through 3 for the next control whose event you want to bind to the method.

When your handler is called, you can determine which control caused the event.

To determine which control caused the event

  1. In the event handler, declare a variable whose type matches the controls that raise the event.
  2. Assign the sender argument of the event handler to the variable, casting it to the appropriate type.

    The following example shows the handler for a Button-control click event that is called by several different buttons. The handler displays the ID property of the button that caused the event.

    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click, Button3.Click
            Dim b As Button = CType(sender, Button)
            Label1.Text = b.ID
    End Sub
    
    // C#
    private void Button1_Click(object sender, System.EventArgs e)
        {
            Button b = (Button) sender;
            Label1.Text = b.ID;
        }
    

See Also

Creating Event Handlers in Web Forms Pages | Binding to an Existing Event Handler in Web Forms Pages | Creating an Event Handler at Run Time in Web Forms Pages | ASP.NET Server Control Event Model | Server Event Handling in Web Forms Pages | Handles | ASP.NET Server Controls | Handling and Raising Events (.NET Framework documentation)

Show:
© 2014 Microsoft