Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net How to get the id of the button user clicked

Tags:

asp.net

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.

like image 894
LIH Avatar asked Jul 10 '13 14:07

LIH


3 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.

like image 182
Geeky Guy Avatar answered Nov 02 '22 23:11

Geeky Guy


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
    }
}
like image 45
Brian Driscoll Avatar answered Nov 03 '22 00:11

Brian Driscoll


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

like image 1
Ameen Chaabani Avatar answered Nov 02 '22 23:11

Ameen Chaabani