Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Determine which Web Server Control Raised an Event

When an event 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 control that raised 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 information about which button was clicked.

    private void Button_Click(object sender, System.EventArgs e)
    {
        Button b;
        b = (Button)sender;
        switch (b.ID)
        {
            case "Button1":
                Label1.Text = "You clicked the first button";
                break;
            case "Button2":
                Label1.Text = "You clicked the second button";
                break;
            case "Button3":
                Label1.Text = "You clicked the third button";
                break;
        }
    }
    
    
    

Community Additions

ADD
Show:
© 2015 Microsoft