Re: How to add eventHandlers at runtime?

Discussion in 'ASP .Net' started by Scott Meddows, Sep 2, 2003.

  1. Do you have any sample code for this? I'm looking for the same thing, but
    the samples don't show how to do this dynamically.

    "Mattias Sjögren" <> wrote in message
    news:e0%...
    > >How to attach a delegate to a Control's event(Lets say click) dynamically

    I
    > >am trying to use EventInfo Clas but in AdEventHandler method takes 2nd
    > >parameter as delegate so how can i attch an method to be calledto that
    > >delegate object.

    >
    > You can use Delegate.CreateDelegate() to create the delegate for your
    > handler method.
    >
    >
    >
    > Mattias
    >
    > --
    > Mattias Sjögren [MVP] mattias @ mvps.org
    > http://www.msjogren.net/dotnet/
    > Please reply only to the newsgroup.
     
    Scott Meddows, Sep 2, 2003
    #1
    1. Advertising

  2. Scott,

    >Do you have any sample code for this? I'm looking for the same thing, but
    >the samples don't show how to do this dynamically.


    using System;

    public delegate void MyDelegate();

    class EventProvider
    {
    public event MyDelegate MyEvent;
    public void RaiseIt() { if ( MyEvent != null ) MyEvent(); }
    }

    class Listener
    {
    public static void Handler()
    {
    Console.WriteLine( "Event handler called" );
    }

    public static void Main()
    {
    EventProvider ep = new EventProvider();
    Delegate d = Delegate.CreateDelegate(
    typeof(MyDelegate), typeof(Listener), "Handler" );
    ep.GetType().GetEvent( "MyEvent" ).AddEventHandler( ep, d );
    ep.RaiseIt();
    }
    }



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/
    Please reply only to the newsgroup.
     
    Mattias Sjögren, Sep 6, 2003
    #2
    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. Kristof Pauwels
    Replies:
    1
    Views:
    436
    Rahul Singh
    Jul 27, 2003
  2. Mathew Quick
    Replies:
    1
    Views:
    324
    John Saunders
    Jun 15, 2004
  3. Timo
    Replies:
    1
    Views:
    375
    Kevin Spencer
    Feb 16, 2005
  4. Martin
    Replies:
    3
    Views:
    788
    Teemu Keiski
    May 14, 2005
  5. Christian Cambier

    how to add webform-eventhandlers ??

    Christian Cambier, Jul 23, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    337
    Mark Fitzpatrick
    Jul 23, 2008
Loading...

Share This Page