handling events raised within a placeholder

Posted on 2005-05-01
Last Modified: 2012-08-14
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,

Question by:michaelwkane
    LVL 33

    Expert Comment

    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.


    Author Comment

    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.
    LVL 33

    Accepted Solution

    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,

    Author Comment

    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 :)


    Author Comment

    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.


    LVL 33

    Expert Comment

    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

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now