Export (0) Print
Expand All
3 out of 4 rated this helpful - Rate this topic

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;
        }
    }
    
    

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.