Event-Driven Programming

and Postback

In ASP.NET, Web forms rely on events to trigger the running of code placed in subroutines. Both HTML and ASP allowed event-driven programming – HTML allowed it through client-side scripting, and ASP allowed it through both client-side and server-side scripting. ASP.NET adds a Postback property, which means that new information is sent to the server for processing whenever there is any change of control values on the form. In this section, we look at some examples of ASP.NET event-driven programming and how the Postback property works.

Events and Event-Driven Programming

Events occur whenever there is any change of state of an object. Examples are: loading a page in the memory, clicking on a button on a page, placing mouse on a text box, unloading a page from the memory, etc. Codes placed in a subroutine can be run upon occurrence of an event. There are in general two kinds of events: built-in events and user-defined events. Built-in events occur whenever there is any change of state of built-in objects; but the user-defined events occur only when there is any action by the user on an object.

ASP.NET Events

A Web page is an object created from the Page class. Whenever a Web page is requested from the Web server, it goes through the stages of initializing, processing, and disposing of information. This generates a series of events on the Browser. Some of the events are:

  • Page_Init: It occurs when a page is initialized. Placing codes in a subroutine such as Sub Page_Init () will run before the system will do other activities such as displaying controls on the form. Page_Init event occurs before Page_Load event.
  • Page_Load: It occurs when the page is read into memory and is visible for the first time. It occurs after the Page_init event.
  • Page_Unload: It occurs when a page is unloaded from the memory. Codes disconnecting a database are typically placed in the Sub Page_Unload subroutine, although ASP.NET typically performs this operation as a routine during the unloading event.

View aspnetevent.aspx

Events in HTML

Events in a Web page can simply be generated by using HTML controls. There are various events that a user can generate upon actions from a mouse or key. For example, we can use a button on a form to generate a variety of events such as:

  • onmouseup: It occurs whenever a mouse is released when the focus is on a control.
  • onmousedown: It occurs whenever a mouse is pressed when the focus is on a control.
  • onmouseover: It occurs whenever a mouse is moved over a control.
  • onmousemove: It occurs whenever a mouse is moved from a control.
  • onclick: It occurs whenever is mouse is clicked over a control.
  • ondblclick: It occurs whenever is mouse is double-clicked over a control.
  • onkeyup: It occurs whenever a key is released over a control.
  • onkeypress: It occurs whenever a key is pressed and released over a control.
  • onkeydown: It occurs whenever a key is pressed and held down over a control.

Program codes placed in a subroutine called by any of these events will be executed. The code below illustrates a simple HTML event triggered by a user.

View htmlevent and click on the button

ASP.NET Server Control Events

We can add events to ASP.NET controls the same way as we would in HTML for client-side controls. However, the ASP.NET controls have a reduced set of events that can be added to controls as extra attributes. They are:

  • onload: occurs when the control has loaded into the window or frame
  • onunload: occurs when a control has been removed from a window
  • onclick: occurs when a mouse is cliked when hovering over the <asp:button> control
  • oninit: occurs when the web page is first initialized
  • onprerender: occurs just before the control is rendered.

In addition to these, there are two events that are handled by the system: selectindexchanged and checkchanged. These occur when the contents of a control have been altered, such as a checkbox being clicked or a list item being selected. They are only applied to controls such as list items and checkboxes.

The large difference between HTML controls and ASP.NET server controls is the way they are dealt with. With HTML form controls, when the event is raised, the browser deals with it.

For the server-side controls, the event is raised by the browser; however, instead of dealing with it, the client raises a trigger telling the server to handle it. In fact, the server only generates a single postback event, which occurs the ASP.NET button control is clicked.

The ASP.NET Button Server Control

The ASP.NET button control is equivalent to HTML <input> tag with type=button. The button control has the following format:

<asp:button id=”id_name” event=”event_handler_name” runat=”server”/>

The ASP.NET button control supports only the following events:

  • oninit
  • onprerender
  • onload
  • onclick
  • onunload

The following code uses the “onclick” event of the ASP.NET button control.

View event.aspx

Postback in ASP.NET

Postback is the process by which the browser posts information back to the server telling the server to handle the event, the server does so and sends the resulting HTML back to the browser again. Postback only occurs with Web forms, and it is only server controls that post back information to the server.

Another way to look at the Postback is through IsPostBack test. This property is used with a page (If Page.IsPostback then...) to test whether the user had returned a form with new data or whether it is in the same state as before. If it hasn’t then this test generates false, otherwise it generates true.

The way the ASP.NET remembers the state of a page is through posting the form to itself and adding a hidden control named _VIEWSTATE. It contains the values of all form controls of the previous state in an encrypted form. Thus ASP.NET can compare the two states (current and previous) without making an extra trip to the server.

One can view the status of _VIEWSTATE by going to the “view source” feature of the browser:

<form name=”ctr10” method=”post” action=”postback.aspx” id=”ctr10”>

<input type=”hidden” name=”_VIEWSTATE” value=”dDw0MDk5MTgxNTU7Oz4=”/>.

Here all attributes of the form control has been added by the systems, where postback.aspx was the name of the original ASP.NET page. The actual value = “.... “ in the hidden control will be different depending on the values of form controls.

Run the following codes and view the source from the browser.

View postback.aspx

The following is a screen shot of the “source” from the browser.

1