[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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,

MK
0
michaelwkane
Asked:
michaelwkane
  • 3
  • 3
1 Solution
 
raterusCommented:
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.

0
 
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.
0
 
raterusCommented:
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
myPlaceHolder.controls.Add(btnDelete).

--

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,
--Michael
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

MK
0
 
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.

Thanks!

MK
0
 
raterusCommented:
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now