The first and foremost thing to learn about the ASP.NET event system is the concept of postback. In ASP.NET, postback is the process by which the browser posts information back to itself (i.e., posts information back to the server by requesting the same page). Postback in ASP.NET only occurs within web forms (i.e., within a form element with runat=server), and only server controls post back information to the server.
The following illustration shows the basic and simplified postback interaction for a simple web page.
More specifically, the processing cycle for an ASP.NET web form is as follows.
- The user requests an ASP.NET web form. In this example, the request uses the HTTP GET method.
- On the server, the page is run, doing any preliminary processing such as compilation (if necessary), as well as calling other handlers as part of the page and application lifecycle (covered later in the chapter).
- The Page_Load method of the page is called.
- The rest of the page executes, which ultimately results in a generated HTML response which is sent back to the browser. Part of this generated HTML is the view state (discussed below) information contained within a hidden HTML input element. As well, the action and method attributes of the element are set so that the page will make a post back request to the same page when the user clicks the Enter button.
- Browser displays the HTML response.
- The user fills in the form then causes the form to post back to itself (perhaps by clicking a button). If the user clicks a link that requests a different page, then the following steps are not performed, since with the new page request, we would return back to step one.
- The page is posted back to the server, usually using the HTTP POST method. Any form values along with the view state information is sent along as HTTP form variables.
- On the server, the page is run (no compilation will be necessary since it will have already been compiled). The ASP.NET run-time recognizes that this page is being posted back due to the view state information. All user input is available for programmatic processing.
- The Page_Load method is called.
- Any invoked control event handlers are called. In this case, a click event handler for the button will be called.
- The generated HTML is sent back to the browser.
- Browser displays the HTML response.
These steps will continue as long as the user continues to work on this page. Each cycle in which information is displayed then posted back to the server is sometimes also called a round trip.
Page and control events
As can be seen from the above diagram, there are two different event types in ASP.NET: page events and control events. When a page request is sent to the server, a specific series of page events are always triggered in a specific order. Control events are associated with particular controls and will be fired in certain circumstances. There are some standard events that all controls share; as well, most controls have unique events particular to that control. For instance, the DropDownList web server control has an event that is triggered when the user selects a new list item.