Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a derivate from the well known memory game. To that end, I intended to code-generate a panel with an array of imagebuttons. Consequently, I intend to change the imageurl of the clicked imagebutton. The setup is summarized in the below few lines of code.
However, I cannot catch the click event on the imagebuttons. Is there anybody that could point me in the right direction ?

What I have tried:

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim intRijen, intKolommen As Int16
        ' DEFINE SIZE OF MEMORY BOARD
        intRijen = 4
        intKolommen = 3
        Dim imgbPrent(intRijen, intKolommen) As ImageButton
        For nR = 0 To intRijen
            For nK = 0 To intKolommen
                imgbPrent(nR, nK) = New ImageButton
                imgbPrent(nR, nK).ID = CStr(nR) & CStr(nK)
                imgbPrent(nR, nK).Width = 100
                imgbPrent(nR, nK).Height = 100
                imgbPrent(nR, nK).ImageUrl = "~/prentjes/" & CStr(nR) & CStr(nK) & ".jpg"
                Panel1.Controls.Add(imgbPrent(nR, nK))
            Next
            Panel1.Controls.Add(New LiteralControl("<br />"))
        Next
    End Sub
    '
    'Protected Sub imgbPrent(0, 0)_click (sender As Object, e As ImageClickEventArgs) Handles Prent.Click
    '     imgbPrent(0,0).ImageUrl = "~/prentjes/99.jpg"
    'End Sub

End Class
Posted
Updated 22-Dec-17 9:16am

You'll need to use AddHandler[^] to attach the event handler.

You should also move the code that creates the controls from the Load event to the Init event. This will ensure that they're created early enough in the page lifecycle to handle post-back events.

And there's no point creating an array of the controls that you're adding as a local variable, since you never use that array anywhere.
VB.NET
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Dim intRijen, intKolommen As Int16
    intRijen = 4
    intKolommen = 3
    For nR = 0 To intRijen
        For nK = 0 To intKolommen
            Dim imgbPrent As New ImageButton
            imgbPrent.ID = CStr(nR) & CStr(nK)
            imgbPrent.Width = 100
            imgbPrent.Height = 100
            imgbPrent.ImageUrl = "~/prentjes/" & CStr(nR) & CStr(nK) & ".jpg"
            Panel1.Controls.Add(imgbPrent)
            AddHandler imgbPrent.Click, AddressOf imgbPrent_Click
        Next
        Panel1.Controls.Add(New LiteralControl("<br />"))
    Next
End Sub

Protected Sub imgbPrent_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
    Dim btn As ImageButton = DirectCast(sender, ImageButton)
    btn.ImageUrl = "~/prentjes99.jpg"
End Sub
 
Share this answer
 
Your "AddHandler" answer did help me out : THANKS !

I had been struggling for about a week, and was getting frustrated, desperate, depressed, and borderline suicidal. Just to emphasise how voluntarist reactions really do help beginners, especially when your answer is to the point, and even motivated and explained. THANKS again !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900