|
-
Jun 2nd, 2005, 09:47 AM
#1
Thread Starter
Fanatic Member
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
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jun 2nd, 2005, 03:20 PM
#2
I wonder how many charact
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.
-
Jun 3rd, 2005, 02:12 AM
#3
Thread Starter
Fanatic Member
Re: Handling events for dynamically created controls
My control is being created in the ItemDataBound event of a repeater control...?
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jun 3rd, 2005, 03:47 AM
#4
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.
-
Jun 3rd, 2005, 03:48 AM
#5
Thread Starter
Fanatic Member
Re: Handling events for dynamically created controls
 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!
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jun 3rd, 2005, 05:27 AM
#6
I wonder how many charact
Re: Handling events for dynamically created controls
 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.
Last edited by nemaroller; Jun 3rd, 2005 at 05:33 AM.
-
Jun 3rd, 2005, 05:36 AM
#7
Thread Starter
Fanatic Member
Re: Handling events for dynamically created controls
Well, thankyou very much!
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jun 3rd, 2005, 05:44 AM
#8
I wonder how many charact
Re: Handling events for dynamically created controls
I guess the universe woke me up at 5 in the morning to help you out
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|