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

Discussion in 'ASP .Net' started by Jose Suero, Dec 21, 2003.

  1. Jose Suero

    Jose Suero Guest

    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
     
    Jose Suero, Dec 21, 2003
    #1
    1. Advertising

  2. Jose Suero

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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
     
    Teemu Keiski, Dec 23, 2003
    #2
    1. Advertising

  3. Jose Suero

    Jose Suero Guest

    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" <> wrote in message news:<#$>...
    > 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" <> wrote in message
    > news:...
    > > 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
     
    Jose Suero, Dec 26, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pavils Jurjans

    Assign session onEnd event handler at run-time

    Pavils Jurjans, Apr 21, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,587
    Pavils Jurjans
    Apr 21, 2004
  2. =?Utf-8?B?bWc=?=

    Generating event handler at run time.

    =?Utf-8?B?bWc=?=, Nov 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    350
    =?Utf-8?B?bWc=?=
    Nov 13, 2004
  3. Replies:
    1
    Views:
    756
    Damien
    Feb 22, 2007
  4. Jeff

    How to add an event handler at run time?

    Jeff, Aug 24, 2004, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    171
    Niranjan
    Sep 15, 2004
  5. HugeBob

    Dynamically Assigning OnChange Handler

    HugeBob, Jun 3, 2008, in forum: Javascript
    Replies:
    2
    Views:
    153
    HugeBob
    Jun 3, 2008
Loading...

Share This Page