4.1. The Browser Page Lifecycle

One of the first things to understand about the ASP.NET AJAX Library is that it establishes a page lifecycle for JavaScript code running in the browser. It is not as complex as the server page lifecycle, but it is central to interacting with some of the more complex features. This lifecycle gives you some easy "hooks" that you need to tap into in order to do the processing you want to do at the right time.

Although the JavaScript event mechanism is simpler than that provided by the .NET Framework, it is sufficient for your needs. Listing 4-1 shows a very basic page built with ASP.NET. This page simply pops up a box with the word Hello, but it shows a basic starting point. To use the Microsoft AJAX Library and initiate the client-side lifecycle in the browser, there is a ScriptManager control on the page. This takes care of rendering the references to the scripts that make up the library, which the browser will have to request when the page loads.

Example 4-1. Working with the pageLoad() and pageUnload() functions
<%@ Page Language="C#" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Simple Hello</title>

    <script type="text/javascript">
        function pageLoad(sender, args) {
           alert("Hello");
        }

        function pageUnload(sender, args) {
           alert("Goodbye");
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    </div>
    </form>
</body>
</html>

The pageLoad() ...

Get Professional ASP.NET 3.5 AJAX 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.