Home  |  Contact us    
Adding Event Handlers to controls


How can add Event Handlers to controls in ASP.NET 2.0?

Most of the code in an ASP.NET web page is placed inside event handlers that react to web control events. Using Visual Studio, you can add an event handler to your code in three ways:

Type it in by hand: In this case, you add the method directly to the page class. You must specify the appropriate parameters so that the signature of the event handler exactly matches the signature of the event you want to handle. Youll also need to edit the control tag so that it links the control to the appropriate event handler. (Alternatively, you can use delegates to wire this up programmatically.)

Double-click a control in design view: In this case, Visual Studio will create an event handler for that controls default event (and adjust the control tag accordingly). For example, if you double-click the page, it will create a Page.Load event handler. If you double-click a Button control, Visual Studio will create an event handler for the Click event.

Choose the event from the Properties window: Just select the control, and click the lightning bolt in the Properties window. Youll see a list of all the events provided by that control. Doubleclick in the box next to the event you want to handle, and Visual Studio will automatically generate the event handler in your page class and adjust the control tag.


The second and third options are the most convenient. The third option is the most flexible, because it allows you to select a method in the page class that youve already created. Just select the event in the Properties window, and click the drop-down arrow at the right. Youll see a list that includes all the methods in your class that match the signature this event requires. You can then choose a method from the list to connect it. The only limitation of this technique is that it works exclusively with web controls, not server-side HTML controls.

Category: ASP.NET 2.0 Faqs, Interview Questions & Answers



Ask New Questions
Microsoft.Net Framework
.Net 2.0 Namespaces
.Net Assemblies
OOPS Faqs in C#
ASP.Net Faqs
ASP.Net 2.0 Faqs
Web Services Faqs
ADO.Net Faqs
Database Faqs
OOPS Faqs
IT Project Management