Events

The two models of program execution (which are not necessarily mutually exclusive) are linear and event-driven. The key to understanding ASP.NET is that it is event-driven.

Linear programs move from step 1, to step 2, and so on, to the end of all the steps. Flow control structures within the code (such as loops, if statements, or method calls) may redirect the flow of the program, but essentially, once the program execution begins, it runs its course unaffected by anything the user or system may do. Before GUI environments, most computer programs were linear.

In contrast, event-driven programming responds to something happening (such as a button being pressed). Most often, events are generated by user action, but events can be raised by the system. For example, the system might raise an event when a file that you open for reading has been read into memory or when your battery’s power is running low.

In ASP.NET, objects may raise events and other objects may have assigned event handlers. For example, a button may raise the Click event, and the page may have a method to handle the button’s click event (such as Button1_Click). Your code in the event handler then responds to the button’s being clicked in whatever way is appropriate for your application.

The main point to remember here is that server controls are objects that can raise events. Any action a user takes with a server control on the browser raises an event. Your server-side code responds to that event, running the ...

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.