0

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

2 Answers 2

2

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 :)

4 Comments

But for Imagebutton text option is not available
Some what ok but i am unable to handle this in button click
Why the comparison with .Text? Just compare the button itself: if (sender == MyButton1) ...
Sorry had a complete mind lapse, I up voted yours to thank you for pointing out my retarded ways :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.