Handling events for dynamically created controls
I need to handle the Command event for an ImageButton web control on an ASPX page in a VB.NET project.
My Imagebutton is being instantiated and added to a place holder control's collection. How can I tell VB which procedure will handle the event?
I have tried using the AddHandler but my popup list of event objects only includes those objects that exist at design time. I have tried refering to my newly created image button anyway, and get no error when the code runs, but it doesn't fire my procedure when it should:
VB Code:
' Add Image Button
ibnConfirm = New ImageButton
ibnConfirm.ImageUrl = "Confirm16.ico"
ibnConfirm.CommandName = "Confirm"
ibnConfirm.CommandArgument = e.Item.DataItem("pk_Quotation_in")
ibnConfirm.ToolTip = "Confirm Quotation"
AddHandler ibnConfirm.Command, AddressOf ibnConfirm_Command
' Add to place holder collection
plhConfirm.Controls.Add(ibnConfirm)
My procedure to handle the Command Event:
VB Code:
Private Sub ibnConfirm_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
End Sub
Re: Handling events for dynamically created controls
Are you recreating the dynamic control on postback?
If you're not, you'll never fire the event, because the event will never get wired.
Re: Handling events for dynamically created controls
My control is being created in the ItemDataBound event of a repeater control...?
Re: Handling events for dynamically created controls
I have a rough idea of what you could try, but no code to show you.
You can create a single sub that accepts the usual arguments that an event's sub does. When you create the control, you can then delegate the event to that sub.
Re: Handling events for dynamically created controls
Quote:
Originally Posted by mendhak
I have a rough idea of what you could try, but no code to show you.
You can create a single sub that accepts the usual arguments that an event's sub does. When you create the control, you can then delegate the event to that sub.
That's exactly what I've done...it just doesn't work! :mad:
Re: Handling events for dynamically created controls
Quote:
Originally Posted by simonm
My control is being created in the ItemDataBound event of a repeater control...?
Then that's your problem. Move the actual creation part to the ItemCreated event instead.
ItemCreated is called everytime the page is postback or requested, whereas ItemDataBound only happens when you call DataBind on the repeater. So in your case, your dynamic control is not being re-created on postback because it was only created when you bound the repeater on the previous request. The next request won't re-created the dynamic control because its not being bound again (which is what you want.)
Normally, you can get away with just using ItemDataBound, because the as a programmer, you have defined a control inside the ItemTemplate on the aspx page... but for a dynamic control you wouldn't have that inside the ItemTemplate, so its not being automatically created for you.
Create the control in the ItemCreated event, bind its properties in the ItemDatabound event.
Re: Handling events for dynamically created controls
Well, thankyou very much! :wave:
Re: Handling events for dynamically created controls
I guess the universe woke me up at 5 in the morning to help you out :)