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.
4 Answers
When you use a button clicked event, the signature of the method that'll handle the event goes like this:
protected 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 it. Like this:
Button button = (Button)sender;
string buttonId = button.ID;
You can have all your buttons pointing their Click
event to the same method like this.

- 9,229
- 4
- 42
- 62
-
@jabu.hlong I saw your edit. It is correct, I don't know why it was rejected. I am correcting my answer here, thank you. – Geeky Guy Sep 08 '14 at 12:08
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
}
}

- 19,373
- 3
- 46
- 65
-
1+1 because it is correct, but I simply had to say this: if you're getting a null reference for the sender in a method called by a `Button.Click` event handler, someone is doing something very sick somewhere. – Geeky Guy Jul 10 '13 at 14:50
-
1Regardless of circumstance it is always best practice to check for null after a safe cast. – Brian Driscoll Jul 10 '13 at 14:51
-
but there is no such keyword "sender"... Sorry I did not understand the code – LIH Jul 10 '13 at 14:56
-
@LIH you use that piece of code inside the method that handles the `Click` event. – Geeky Guy Jul 10 '13 at 14:58
-
@LIH I've just updated my code example to make it more obvious that it's the body of an event handler. – Brian Driscoll Jul 10 '13 at 14:58
I think there's another way to do this method you may not thought of it, why don't you put an
Image control
you only need switch the image's color for this method, the first one we assume that it's Red and it means free seat, and the second one is Blue and means taken .
here is a link you may need to know about this issue How to change color of Image at runtime
Hope I could gave you a better idea. thank you

- 1
- 1

- 191
- 1
- 1
- 11
-
Not the answer the OP or anyone else was thinking about, but +1 for the idea. This is way better in terms of user friendliness and usability. – Geeky Guy Jul 10 '13 at 17:37
-
BTW he would still need to know how to fetch data from the button even if he did it this way. – Geeky Guy Jul 10 '13 at 17:38
I know this is an old thread but hopefully it helps someone out there having the same trouble I was here in 2019! This is my solution using the Tag property of the button with Binding through XAML. Worked great for me, even though it might be a little hacky:
In your button click event:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button EditButton = (Button)e.Source;
int id = Convert.ToInt32(EditButton.Tag);
//do your stuff
}
In your XAML:
<Button Click="Button_Click" Content="Edit" Tag="{Binding whateverInt}" />

- 1
- 1