Hi,

I am creating a page in which thumnails, in the form of Image Buttons (System.Web.UI.WebControls.ImageButton), are dynamically created based on which LinkButton is clicked out of a group of several links. I need each thumbnail to generate a full-size image on the page when clicked. I would use the ImageButton_Click event, but I need arguments other than X and Y positions. So I decided to use the _Command event, which is described (just as the _Click event) to fire when the button is clicked. This event gives me a CommandName and a CommandArgument to work with; however, it will not work. I have put both break points and a response.write() into the _Command event's stub and nothing occurs to indicate that it runs, though the _Click event does occur. My code is below:

Sub showThumbs(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim a As Integer = 0
Do
tblEvents.Rows(a).BackColor = Drawing.Color.White
tblEvents.Rows(a + 1).BackColor = Drawing.Color.LightBlue
a += 2
On Error Resume Next
Loop While a < RowNum
tblEvents.Rows(e.CommandName).BackColor = Drawing.Color.Aquamarine
tblEvents.Rows(e.CommandName).ID = "Selected"
Dim Conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
Dim Cmd As New Data.SqlClient.SqlCommand("SELECT event_photo_id FROM EVENT_PHOTO WHERE event_id =" & e.CommandArgument, Conn)
Dim EventRead As Data.SqlClient.SqlDataReader
Conn.Open()
EventRead = Cmd.ExecuteReader
Dim Thumb As ImageButton
Dim TR As TableRow
Dim TC As TableCell
Dim x As Integer = 0
Dim y As Integer = 0
While EventRead.Read
x += 1
If x = 4 Then x = 1
If x = 1 Then
y += 1
If y = 3 Then y = 1
TR = New TableRow
If y = 1 Then
TR.BackColor = Drawing.Color.White
Else
TR.BackColor = Drawing.Color.LightBlue
End If
End If
TC = New TableCell
TCThumbInit.Visible = False 'hide initial information
Thumb = New ImageButton
Thumb.CommandArgument = e.CommandArgument
Thumb.CommandName = e.CommandName
AddHandler Thumb.Command, AddressOf Me.showFull
Thumb.ImageUrl = "~/IMG.aspx?Type=2&ID=" & EventRead(0)
Thumb.Width = "100"
TC.Controls.Add(Thumb)
TC.Controls.Add(New LiteralControl("&nbsp; &nbsp;"))
TR.Cells.Add(TC)
If x = 3 Then tblThumb.Rows.Add(TR)
End While
If x <> 3 Then tblThumb.Rows.Add(TR)
Conn.Close()
End Sub
Sub showFull(ByVal sender As Object, ByVal e As CommandEventArgs)
'showThumbs(sender, e)
Response.Write("imageButton.Command")
'Dim FullIMG As New Image
'FullIMG.ImageUrl = "~/IMG.aspx?Type=2&ID=" & e.CommandArgument
'TCFull.controls.add(FullIMG)
End Sub

The showThumb() sub procedure is called as the LinkButton_Command event and the ShowFull() sub is assigned to the ImageButton_Command event in the line: AddHandler Thumb.Command, AddressOf Me.showFull

Okay, I've broken this down to make the problem as clear as I can. Here is the code I have written for a simple page with one table called Table1(with one row and one cell), and a Button called Button1

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Sub imgClick(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Response.Write("Image Click")
End Sub
Sub imgCommand(ByVal sender As Object, ByVal e As CommandEventArgs)
Response.Write("Image Command")
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim img As New ImageButton
img.ImageUrl = "~/t_can.gif"
AddHandler img.Click, AddressOf Me.imgClick
AddHandler img.Command, AddressOf Me.imgCommand
Table1.Rows(0).Cells(0).Controls.Add(img)
End Sub
End Class

The lines AddHandler img.Click, AddressOf Me.imgClick and AddHandler img.Command, AddressOf Me.imgCommand add the events for the image button. When this code is done to an image button that is either already on the form, or is generated during the Form_Load event, this works correctly and the actions in the imgClick and imgCommand subs are executed when the image is clicked; however, if the button is generated by another event such as a button's click event, this does not work. Why does this happen and how might I be able to fix it?

hi chimpus dupus...
i have same problem with u...
n i have posting in many forum n no one can resolve it... OMG...
the problem is just simple as you say...
i create imagebutton n it's click event...
but when i click the imagebutton, the event not getting called...

my code is :

Protected Sub Gerak(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        MsgBox("Masuk", MsgBoxStyle.OkOnly)
    End Sub


For i = 0 To jum - 1
            img(i) = New ImageButton()
            AddHandler img(i).Click, AddressOf Me.Gerak
            img(i).ImageUrl() = "images/kodok-hijau.gif"
            pGame.Controls.Add(img(i))
        Next

when i click imagebutton, it won't show the messagebox....

maybe you can try using button... my friend tell me that she use button with the same method and work...

This article has been dead for over six months. Start a new discussion instead.