Dynamic ImageButton EventHandler

bairdmw

Newcomer
Joined
Oct 27, 2009
Messages
4
I am creating dynamic ImageButtons (ie. a print icon for each row of a dynamic table). I can format the buttons and get them to PostBack, but I cannot work out how to handle the button click event.

Code:
imgPrint = New ImageButton
With imgPrint
    .CausesValidation = True
    .CommandName = "imgButton"
    .CommandArgument = drRuns("SchoolRunID")
    .ToolTip = "Print"
    .ImageUrl = "~/images/print.gif"
End With

I can't create an imgButton_onClick sub in the code behind file as imgButton is created dynamically at runtime, so it doesn't recognise this control/event in the code behind. I have got the following code in the Page_Load sub;

Code:
If Not IsPostBack Then
    ' do some other stuff here 
Else
    ' this is where I want to know if an instance of imgButton was clicked
    ' how do I do it??
End If

Can someone fill in the blanks for me please?

THanks!
 
I have added the AddHandler statement and played around with a few other things, but still can't get this to work.

Code:
With imgPrint
    .CommandName = "imgPrint_" & drRuns("SchoolRunID")
    .CommandArgument = drRuns("SchoolRunID")
    .AlternateText = "Print" : .ToolTip = "Print"
    .ImageUrl = "~/images/print.gif"
    AddHandler .Click, AddressOf imgPrint_Click
End With

Private Sub imgPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("here")
    Response.End()
End Sub

The image buttons all format and display correctly, but when I click one I get a PostBack but the imgPrint.Click event does not fire the imgPrint_Click sub.
 
Which event are you adding the buttons in?

The buttons are being added as the page loads. I have a table which I am adding rows to at runtime. Each row has an ImageButton.

The page is loading correctly and the buttons cause the page to PostBack, but I'm not sure how to catch the event when the PostBack occurs.
 
Back
Top