Hari Gillala
Hari Gillala

Reputation: 11926

Button Click Asp.Net

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

Upvotes: 1

Views: 706

Answers (5)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263147

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

Upvotes: 3

Wesley
Wesley

Reputation: 800

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.

Upvotes: 0

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25475

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.

Upvotes: 0

Geoff Appleford
Geoff Appleford

Reputation: 18832

Convert the sender parameter to a button and check its ID

Upvotes: 0

skaz
skaz

Reputation: 22640

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?

Upvotes: 1

Related Questions