如何在按钮单击事件下处理图像单击

时间:2010-08-21 11:01:31

标签: c# asp.net

大家好我有两个图像按钮,一个gridview和一个按钮。现在,如果我点击图像按钮,我将显示一个网格。现在在按钮下单击我想捕获单击了哪个图像按钮如果单击第一个图像按钮我想要一些值,如果点击第二个我想显示另一个

2 个答案:

答案 0 :(得分:2)

您可以通过比较sender参数:

来辨别按下了哪个按钮
void MyButton_Click(object sender, EventArgs e) 
{ 
    if (sender == MyButton1)
    {
        // 1st image button was clicked — some values
    }
    else if (sender == MyButton2)
    {
        // 2nd one was clicked — show another
    }
}

答案 1 :(得分:0)

你不能创建2个事件和一个功能吗? 例如:

//Hook both OnClick events to these!    
private void OnButton1Click(object sender, EventArgs e) { BeenClicked(button1); }
private void OnButton2Click(object sender, EventArgs e) { BeenClicked(button2); }

private void BeenClicked(Button ClickedButton) 
{
    if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!"); 
}

或者您也可以使用:

//Hook both OnClick events to this!
private void OnButtonClick(object sender, EventArgs e) 
{ 
    ClickedButton = (Button)sender; 
    if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!"); 
}

如果我理解你的话:)