确定引发事件的Web服务器控件

1、在事件处理程序中,声明类型与引发事件的控件匹配的变量。将
2、事件处理程序的 sender 参数分配给变量,将它强制转换为适当的类型。

下面的示例演示由几个不同按钮调用的 Button 控件 click 事件的处理程序。该处理程序显示了与单击按钮有关的信息。

private void Button_Click(object sender, System.EventArgs e)
{
    Button b;
    b = (Button)sender;
    switch (b.ID)
    {
        case "Button1":
            Label1.Text = "You clicked the first button";
            break;
        case "Button2":
            Label1.Text = "You clicked the second button";
            break;
        case "Button3":
            Label1.Text = "You clicked the third button";
            break;
    }
}

原文地址:https://www.cnblogs.com/zhangpengshou/p/1202889.html