1 out of 1 rated this helpful - Rate this topic

Creating an Event Handler at Run Time in Web Forms Pages

Visual Studio .NET 2003

If you already have an event handler with the appropriate signature, you can bind a control event to it at run time.

To create an event handler at run-time in Web Forms using Visual Basic

  • Include an AddHandler statement, passing it the event to bind and the address of the method to call. Be sure that the statement is executed before the event can be raised — typically, you add handlers in the page initialization. The following example shows how you can bind the Click event of the Button1 control a method called myEventHandler:
    AddHandler Button1.Click, AddressOf myEventHandler
    

To create an event handler at run-time in Web Forms using Visual C#

  • Create an instance of the EventHandler delegate, passing it the address of the method to bind to. Then add the delegate object to the list of methods called when the event is raised. The following example shows how you can bind the Click event of the Button1 control to a method called myEventHandler:
    Button1.Click += new System.EventHandler(this.myEventHandler);
    

See Also

Creating Event Handlers in Web Forms Pages | Binding to an Existing Event Handler in Web Forms Pages | Connecting Multiple Events to a Single Event Handler in Web Forms Pages | ASP.NET Server Control Event Model | Handles | Server Event Handling in Web Forms Pages | ASP.NET Server Controls | Handling and Raising Events (.NET Framework documentation)

Did you find this helpful?
(1500 characters remaining)