handling events raised within a placeholder

Heyy . . I'm making a community site, and each person's profile can have a number of pictures.

When editing a profile, there's a section for pictures, and each picture is represented by a user control that displays the image, the caption, and a "delete photo" button.

Say someone clicks the "delete photo" button --- I want to capture that event in the containing page.  I've seen references to "RaiseBubbleEvent()" which seems like the right idea, but I'm not sure how that will work.

The problem I envision is that 1) I don't know what the parent object is - is it the placeholder?  'Cause if it is, the placeholder doesn't seem to have many events associated with it.  2) how do I determine which user control fired the event?  Should that be specified in the EventArgs?  Does a control that's added dynamically to a placeholder know it's own place in the placeholder's controls collection?  I ask because, if someone deletes a photo, I might as well remove the associated control from the placeholder, but I'm not sure how to determine its position in the collection.

Any ideas?

Thank you,

Who is Participating?
raterusConnect With a Mentor Commented:
Right, that is ok,

The only problem with "dynamic" controls, and events like you want is you must recreate them when the page loads on a postback, or any posted values/events will not be restored to the control.  Also, in order to raise the events from dynamic controls, you do not have "handles myButton.Click" rather, you must use AddHandler, and just reference a valid Sub in your project, it looks something like this:

Dim btnDelete as Button = New Button
AddHandler btnDelete.Click, AddressOf Delete_Picture


Putting all this aside, have you ever thought to use a Repeater/Datalist for this display, it is much easier to customize the <ItemTemplate> of these controls with the display you want, rather than messing with dynamic controls.

Hope something of this helps,
Any event raised should have "sender as object", in the handler, this refers to the control that raised the event.  If you handle this event first in the usercontrol, raised from a button, then raise a custom event from the usercontrol to the page, you should be able to get a direct reference to this usercontrol.

michaelwkaneAuthor Commented:
Thanks, raterus . .

When I try to declare an event handler in the containing page, I run into this problem: I can't write an event handler that handles "ExplicitlyDeclaredControl.Event", because I haven't explicitly declared these controls.  They're added to a placeholder dynamically, and when I write the "Handles" word, intellisense only allows me to choose from the list of controls that have been declared.
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

michaelwkaneAuthor Commented:
Awesome -- thanks, Michael.  I wound up using explicitly-declared user controls, 'cause there's a predetermined number I need.  

However, this is good to know for future reference.  User controls seem like the right solution for this particular part of my project, but I'll definitely be using datalists for search results and the like.

Thanks again :)

michaelwkaneAuthor Commented:
Hey, Michael . . one question: do you know how to use AddHandler with a dynamically added control?  Each time I type AddHandler, intellisense offers a list of the declared controls.

I thought I had the problem fixed by using a finite number of controls without a placeholder, but they're acting very strangely.  Long story short, I'm thinking of using placeholders again, but will need to add event handlers to the controls therein.

Anyway, if you have any suggestions, I'd love to hear them.


Intellisense is a little weird after you type AddHandler, it only will list publically declared controls, but you can use dynamic controls like this,

Dim b as Button = new Button()
AddHandler b.Click, AddressOf My_ButtonClick

Private Sub My_ButtonClick(sender as object, e as EventArgs)
  'some button code
End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.