Developer
Developer

Reputation: 8636

How to handle image click under Button click event

Hi all i am having 2 imagebuttons a gridview and a button. Now if i clicked on Image button i will show a grid. Now under button click i would like to capture which image button was clicked if 1st image button is clicked i would like to some values and if 2nd one is clicked i would like to show another

Upvotes: 0

Views: 1498

Answers (2)

Blam
Blam

Reputation: 2965

Can't you create 2 events and one function? ex:

//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!"); 
}

Or alternatively you could use:

//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!"); 
}

If I understood you right :)

Upvotes: 0

Timwi
Timwi

Reputation: 66573

You can discern which button was pressed by comparing the sender parameter:

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

Upvotes: 2

Related Questions