PC Review


Reply
Thread Tools Rate Thread

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

 
 
Jose Suero
Guest
Posts: n/a
 
      21st Dec 2003
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      23rd Dec 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Jose Suero
Guest
Posts: n/a
 
      26th Dec 2003
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" <(E-Mail Removed)> wrote in message news:<#$(E-Mail Removed)>...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to stop a paint event handler function by another event wasishincar Microsoft Dot NET Framework 4 15th Jan 2008 05:49 AM
Passing arguments to a function in an event handler sjg1314@gmail.com Microsoft Access Forms 2 5th Jul 2006 02:04 PM
Newbie Questions: Dynamically assigning OnClick function Ted Microsoft Dot NET 0 6th Dec 2004 06:59 PM
Function as event handler Gary Schuldt Microsoft Access Forms 20 22nd Oct 2004 06:09 PM
Calling a static function in a dynamically loaded assembly (DLL) =?Utf-8?B?Sm9uYXRoYW4gUm9ld2Vu?= Microsoft C# .NET 4 9th Feb 2004 09:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:40 PM.