4

So i need to write simple "Minesweeper" game in asp.net (Only for learning purposes - it should be almost only javascript but anyway...)

I'm creating the game board dynamically at PageLoad, by creating a Table when each TableCell contains an ImageButton.

When i create each button, i'm adding to it's Click event my own event handler:

cellButton.Click += new EventHandler(tryOpenCell);

When i'm running the project, the game page loading exactly as expected, but when i click on any cell (which is an ImageButton as i said) the request goes back to the server code, but the button's Event Handler never invoked. Instead, the whole process just repeat itself, means regenerating the whole table game.

So my question is why my event handler never invoked?

0

1 Answer 1

2

On Postback your button should be recreated and then it will handle the event..

protected void Page_Load(object sender, EventArgs e)
{
        ImageButton btn = new ImageButton();
        btn.ID = "Btn";
        btn.Click += new EventHandler(tryOpenCell);
        form1.Controls.Add(btn);
 }

Your button should be recreated in the page load event, before it can handle the event handler.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess that the most important thing is to specify the same ID when originally creating the button and at postback?
Your button should be recreated in page load event, before it handle the event hanlder. I have tested this thing at my end. As you know normally when you click button, page load event called before click event handler.
Thanks! i think i had a gap in the matter of how event-deriven programming works on ASP.Net. Now it makes more sense.

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.