1

I have three buttons which have different names and have a same click Event.

How can distinguish one click from other click, as depending on the button selected I need perform various actions.

Thank you

5 Answers 5

2

You can use the sender argument passed to your event handler.

Assuming you have three buttons with IDs Button1, Button2 and Button3:

protected void Buttons_Click(object sender, EventArgs e)
{
    if (sender == Button1) {
        // Do something...
    } else if (sender == Button2) {
        // Do something else...
    } else if (sender == Button3) {
        // Etc.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The "object sender" argument of the event handler will be the button - you can check to see which one it is. If you really need to differentiate, why not have 3 handlers?

Comments

0

Convert the sender parameter to a button and check its ID

Comments

0

In you click method you'll need a switch/select statement. Then you could use the sender object like the c# example below.

public buttons_click(Object sender, Event e)
{
    var buttonText = sender.Text;
    switch(buttonText)
    {
        case "button1":
            //code
            break;
        case "button2":
            ect...
    }
}

Hope this helps.

Comments

0

If the buttons have the same object name, but diffirect values, you could use the button value/text to check wich buttons is wich.

I'll edit example in a bit.

Correct me if i am wrong.

Hope this helps, Wesley.

Edit: nvm, someone beat me to the punch haha.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.