467,923 Members | 1,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Add Event Handler Dynamically, but assigning function name at run-time, not to static function

Hi all
I have a dynamically created button, I can add an event handler with:

AddHandler button.click, AddressOf static_function

This works great, but what I need is to create a function that takes
the control, the event and the function as parameters, something like:

function addevent(control as object, event as string, functionname as
string)
AddHandler control.event, addressof functionname
end function

A solution for this would be to add a single function that handles all
buttons and select the correct action thru commandarguments, that
would work, but it's really not what i'm looking for.
Thanks in advance,

Jose Suero
Nov 18 '05 #1
  • viewed: 3401
Share:
2 Replies
Hi,

you cannot pass the functionname as such as AddressOf expects a delegate
type, not function name as string. You could try to use reflection as this
article explains:

http://www.123aspx.com/redir.aspx?res=31050

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Jose Suero" <ms@mstn.com> wrote in message
news:fd**************************@posting.google.c om...
Hi all
I have a dynamically created button, I can add an event handler with:

AddHandler button.click, AddressOf static_function

This works great, but what I need is to create a function that takes
the control, the event and the function as parameters, something like:

function addevent(control as object, event as string, functionname as
string)
AddHandler control.event, addressof functionname
end function

A solution for this would be to add a single function that handles all
buttons and select the correct action thru commandarguments, that
would work, but it's really not what i'm looking for.
Thanks in advance,

Jose Suero

Nov 18 '05 #2
Thanks a lot for your post, It took me on the right track, I had to
change tiny things, since I was getting an error but this works for
imagebuttons, binding an event for a dynamic created object from a
class library

Public Shared Function addevent(ByVal controlname As String, ByVal
type As String, ByVal eventname As String, ByVal procedure As String)
Dim control As System.Web.UI.WebControls.ImageButton
control = page.findcontrol(controlname)
Dim p As Object
Dim DLLName As String = "projectname"
Dim asm As [Assembly] = [Assembly].Load(DLLName)
Dim t As Type = asm.GetType(DLLName & ".WebForm1", True)
p = Activator.CreateInstance(t)
Dim mymethod As MethodInfo = t.GetMethod(procedure)
' get a reference to the EventInfo for this object
Dim pEv As EventInfo = control.GetType.GetEvent(eventname)
' create a delegate to the local procedure
Dim pDel As [Delegate] =
[Delegate].CreateDelegate(pEv.EventHandlerType, mymethod)
' make the local procedure a handler for the event
pEv.AddEventHandler(control, pDel)
End Function

"Teemu Keiski" <jo****@aspalliance.com> wrote in message news:<#$**************@TK2MSFTNGP11.phx.gbl>...
Hi,

you cannot pass the functionname as such as AddressOf expects a delegate
type, not function name as string. You could try to use reflection as this
article explains:

http://www.123aspx.com/redir.aspx?res=31050

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Jose Suero" <ms@mstn.com> wrote in message
news:fd**************************@posting.google.c om...
Hi all
I have a dynamically created button, I can add an event handler with:

AddHandler button.click, AddressOf static_function

This works great, but what I need is to create a function that takes
the control, the event and the function as parameters, something like:

function addevent(control as object, event as string, functionname as
string)
AddHandler control.event, addressof functionname
end function

A solution for this would be to add a single function that handles all
buttons and select the correct action thru commandarguments, that
would work, but it's really not what i'm looking for.
Thanks in advance,

Jose Suero

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andy Read | last post: by
7 posts views Thread by Rob | last post: by
2 posts views Thread by Donal McWeeney | last post: by
7 posts views Thread by Girish | last post: by
2 posts views Thread by Deepesh | last post: by
1 post views Thread by Chris | last post: by
5 posts views Thread by =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.