开发者

Button Click Asp.Net

开发者 https://www.devze.com 2023-01-30 02:09 出处:网络
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

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


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


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?


Convert the sender parameter to a button and check its ID


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消