Create Dynamic Event Handler

G

Guest

Hi,

Could anybody point me in the direction of how to dynamically allocate
an event handler for the custom event of a user control (I'm currently
using VB.NET under .NET 2.0 on Visual Studio 2008 developing a web
application)? I'm starting to tear my little remaining hair out!

I have a user control which contains an event (NavListEventArgs is
derived from EventArgs)...
Public Event NavClick(ByVal sender As Object, ByVal args As
NavListEventArgs)

I have an .aspx page which has the following event handler in the code-
behind...
Protected Sub navList_Click(ByVal sender As Object, ByVal args As
NavListEventArgs)

How do I get the "OnNavClick" (in the following control definitions)
to wire-up the "NavClick" event to the "navList_Click" event handler??
<uc1:NavList runat="server" id="nav1" OnNavClick="navList_Click"/>
<uc1:NavList runat="server" id="nav2" OnNavClick="navList_Click"/>

I'm familiar with the "AddHandler xx.xx, AddressOf xx", but I don't
want to have that restriction.

Many thanks for any advice,
Tom
 
O

OmegaSquared

Hello, Tom,

I don't know what sort of restrictions you are trying to avoid. But if you
wanted to wait until run-time to select which event handler to use you could
create a variable of type Delegate and assign that in your AddHandler
statement. For example, something like the following (untested) code:

Public Delegate Sub navListClickHandler(ByVal sender As Object, ByVal
args As
NavListEventArgs)
....
....
navListClickHandler = AddressOf SomeSelectedHandler
....
....
AddHandler MyObject.NavClick, navListClickHandler

Cheers,
Randy
 
T

Tom

Thanks for the reply Randy,

It's not so much of a restriction, more of an irritant.
I'd just like to know how to replicate the event wireup that you have
with things like the <asp:Button> where you have the
OnClick="btn_Click", and that wires up directly to the function
"btn_Click" without the need for the "AddHandler" code in the
background.

How do you set-up a usercontrol to take the OnClick string property
and attach itself to the event handler of its parent?

Cheers for your help,
Tom
 
O

OmegaSquared

Hello, Tom,

If you really need to do this, I think that you can probably do it using
reflection.

Sorry, but I don't have much experience with this. (Just enough to know it
is a bit convoluted.) I have some vague recollection that you need to look
(in the Type that declares the event) for a Field that has the name of the
event suffixed with the string "Event". Then you can use the SetValue method
on this field to "wire up" an event handler by assigning an object of type
EventInfo.

I would start by studying the Type.GetEvent method and the EventInfo type,
doing some internet searches, and hoping for a response from someone more
enlightened than I about such things.

Good luck,
Randy
 
T

Tom

Thanks Randy, your reply has been very useful :)

Using reflection (as suggested), the following does what I'm after
(I've removed lots of checking code for simplicity here)...

Imports System.Reflection
Dim onClickName as string = "list_NavClick"
Dim eventName as string = "NavClick"
Dim miHandler As MethodInfo = Me.Parent.GetType().GetMethod
(onClickName, BindingFlags.Public Or BindingFlags.Instance)
Dim eventNavClick As EventInfo = Me.GetType().GetEvent(eventName)
Dim miDelegate As [Delegate] = [Delegate].CreateDelegate
(eventNavClick.EventHandlerType, Me.Parent, miHandler)
eventNavClick.AddEventHandler(Me, miDelegate)

Perfect :)

Cheers,
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top