posted by 네코냥이 2014. 1. 23. 14:05

I have a small problem. I am developing an online seat reservation system which allow users to click and select seats. I am using buttons for seats. I am writing a method for the button click event for all buttons. I need to know how to identify the id of the button, user clicked. thanks in advance.

share|edit|flag
add comment

When you use a button clicked event, the signature of the method that'll handle the event goes like this:

private void foo (object sender, EventArgs e)

sender in this case is the button that was clicked. Just cast it to Button again and there you have. Like this:

Button button = (Button)sender;
int buttonId = button.ID;

You an have all your buttons pointing their Click event to the same method like this.

share|edit|flag
1
 
Thanks. It worked –  LIH Jul 10 '13 at 15:02
add comment

The first parameter of your event handler (object source or sometimes object sender) is a reference to the button that was clicked. All you need to do is cast it and then you can retrieve the id value, e.g.:

void Button_Click(object sender, EventArgs e)
{
    Button button = sender as Button;
    if (button != null)
    {
       //get the id here
    }
}