How to detect which button was clicked in code behind?

前端 未结 2 1705
一整个雨季
一整个雨季 2020-12-31 17:41

I have three buttons each calling btn_Clicked on their onClick event. In code behind I want to get the ID of the button that caused postback. I kno

2条回答
  •  灰色年华
    2020-12-31 18:25

    Check whether the sender argument of your callback method is the same reference as the button your are interested in.

    Button button1;
    Button button2;
    
    void OnClick(object sender, RoutedEventArgs args)
    {
        Button button = sender as Button;
        if (button == button1)
        {
            ...
        }
        if (button == button2)
        {
            ...
        }
    }
    

提交回复
热议问题