catch click event from runtime created imagebutton on webform

On a webform I am creating an array of imagebuttons that holds different images collected from a directory. How can I catch the click event from one of these imagebuttons and store the name of the image before returning?

Below is the code to create them. Ideas/CodeSnippets on how to register event and catch the buttonclick would be appreciated !
Dim Currentfile As String
Dim xx As String
Dim Ff() As String
Dim i As String
iPanel = -1
 
For Each Currentfile In Directory.GetFiles(Session("imageDir")+ "images\")
            Ff = Split(Currentfile, "\")
            Currentfile = Ff(Ff.Length - 1)
            xx = Mid(Currentfile, Currentfile.Length - 3, 4)
            If xx = ".jpg" Or xx = ".gif" Then
 
                iPanel = iPanel + 1
 
                Pics(iPanel) = New ImageButton
                Pics(iPanel).ImageUrl = "images\" + Currentfile
                Pics(iPanel).AlternateText = Currentfile
 
                PicturePanel.Controls.Add(Pics(iPanel))
       End If

Open in new window

dmveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bill-HansonConnect With a Mentor Commented:
You just need to define a click handler function, then set the OnClick member of the ImageButton.  I'm not sure how to get the name of the control that was clicked, though.
' The event handler.
Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs) 
	Label1.Text = "You clicked the ImageButton control at the coordinates: (" & _ 
		e.X.ToString() & ", " & e.Y.ToString() & ")"
End Sub
 
' In your code...
If xx = ".jpg" Or xx = ".gif" Then
 
	iPanel = iPanel + 1
	Pics(iPanel) = New ImageButton
	Pics(iPanel).ImageUrl = "images\" + Currentfile
	Pics(iPanel).AlternateText = Currentfile
	Pics(iPanel).OnClick = ImageButton_Click
	PicturePanel.Controls.Add(Pics(iPanel))
	
End If

Open in new window

0
 
dmveAuthor Commented:
Sorry for the late respons. Thanks for tip. Did not fully solve the question. I Only have the OnClientClick event. And when trying to use it, it complaints over this line "Pics(iPanel).OnClick = ImageButton_Click" that is says MUST BE "Pics(iPanel).OnClick = ImageButton_Click()"

I will play a little more with it and come back later on this issue... So I'll keep it on hold for another few days...
0
 
dmveAuthor Commented:
Thanks ;-) Got it working now.
0
All Courses

From novice to tech pro — start learning today.