Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Handlers On-The-Fly

Status
Not open for further replies.

HRoarke

Programmer
Apr 27, 2004
64
US
OK, this may very well be a dumb question (I do very little ASP coding), but here goes:

I have a web user control. Initially it has one control, an image button, that is the "background" of the control. The containing page can add "items" to the control (the "item" is defined by a class within the control) and when an item is added, the control creates a new ImageButton object to represent the newly added item.

Now, what I need to do is to wire up the Click event of the newly added ImageButton. I can catch the Click event of the initial (background) image button (in this case, using AddHandler syntax) with no problem; however, I can't seem to find a way to capture the event of the dynamically added item(s).

A few more details:
I declare two private variables of type ImageButton WithEvents - one for the background and another to represent the dynamically added imagebuttons, e.g.
Code:
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing

<DefaultProperty("Text"), ToolboxData("<{0}:MyControl runat=server></{0}:MyControl")> _
    Public Class MyControl
    Inherits System.Web.UI.WebControls.WebControl

    Private WithEvents img As System.Web.UI.WebControls.ImageButton = New System.Web.UI.WebControls.ImageButton
    Private WithEvents imgIcon As System.Web.UI.WebControls.ImageButton

... then, on Init
    With img
        ...set some display properties, etc. 
    End With
    Me.Controls.Add(img)
    AddHandler img.Click, AddressOf IconClick

When an item is added by the containing page, we create a New instance of imgIcon and hook up the handler, e.g.

Code:
imgIcon = New System.Web.UI.WebControls.ImageButton
    With imgIcon
        ... set display properties, etc.
    End With
    Me.Controls.Add(imgIcon)
    AddHandler imgIcon.Click, AddressOf IconClick

The newly added image buttons have a z-index of 1+ the z-index of the background. However, I can't catch the Click event of the imgIcon objects, and when they're clicked, since they are "above" the background, the background Click event clearly doesn't fire either. I've also tried adding the dynamically created ImageButtons as children of the background imagebutton (in the above code, they're added to the usercontrol.controls collection directly). I've seen some mention of bubbling events, but have found no way to use that in this case. Fundamentally, I just want the (x,y) mouse coordinates..... :-(

Two stars to anyone that can help me wire up this event....

Thanks in advance.

"I swear by my life and my love of it that I will never live for the sake of another man, nor ask another man to live for mine."
— John Galt
Atlas Shrugged

If you want to get the best response to a question, please check out FAQ222-2244 first

 
This code
Code:
imgIcon = New System.Web.UI.WebControls.ImageButton
    With imgIcon
        ... set display properties, etc.
    End With
    Me.Controls.Add(imgIcon)
    AddHandler imgIcon.Click, AddressOf IconClick

is happening where? After or within Page_Load? If so, I'll bet it's too late. Try it in the Init event.

[pipe]
Share your knowledge! -
 
It usually doesn't matter where in the page lifecycle you add a handler for an object. However, VB is simply awful (and IMHO down right buggy) when it comes to dynamically added controls and handling their events.

Two suggestions come to mind:

(1) If you already have imgIcon declared in the class, then just go ahead and add an event handler for it using the Handles keyword. eg:

protected sub OnImgIconClick(...) Handles imgIcon.Click
....

Maybe it will work, I'm not sure.

(2) Use C# to code this control. It is much better at handling dynamically added objects. Programming against them is much more intuitive.

In all honesty, the buggy behavior of VB w/ regards to dynamically created objects is the reason that we are no longer a VB shop. We jumped on the C# bandwagon about 2.5 yrs ago.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Dragonwell -

The AddHandler for imgIcon is indeed called on Init (it's a user control, not a page, so it's the the Init of the web user control - don't know if this matters....)

Link9 - I do indeed add the handler to the imgIcon on Init, but it doesn't work. It's the darndest thing.

I did come up with a workable, if terribly un-elegant, solution: I've placed a blank, transparent ImageButton "layer" on top of the background and "icons". This gets me where I want to be regarding the mouse co-ordinates, but I lose the mouse-over alt-text of the "icons" which I (and my clients) quite liked, so I really don't like the solution....

As before, two stars to whomever has the answer.....

Thanks again -

"I swear by my life and my love of it that I will never live for the sake of another man, nor ask another man to live for mine."
— John Galt
Atlas Shrugged

If you want to get the best response to a question, please check out FAQ222-2244 first

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top