Like the server-side of ASP.NET, the ASP.NET AJAX client
library utilizes namespaces, classes, enumerations, and interfaces using the
same constructs that JavaScript already utilizes. All of this will be
discussed later; however, it's important to know that the framework already
includes a series of namespaces for you. The two biggest ones you'll see are
the Sys and Sys.UI namespaces.
One of the core objects used in the application is the
Application object, in the Sys namespace. This is a core object that is
responsible for the client components within the application, as well as
handling the page lifecycle events and the disposal of objects.
To tap into the load and unload event on the client-side of
the page, use the following JavaScript:
Listing 1
var myLoadHandler = Function.createDelegate(this, this.myLoadCallback);
var myUnloadHandler = Function.createDelegate(this, this.myUnloadCallback);
Sys.Application.add_load(myLoadHandler);
Sys.Application.add_unload(myUnloadHandler);
function myLoadCallback() { }
function myUnloadCallback() { }
When the load event fires on the client side, the
myLoadCallback method runs. Another common usage of the Application class is
to notify that a custom script has been loaded. When looking at AJAX control extender scripts, you will see this at the bottom:
Listing 2
if (typeof(Sys) !== 'undefined')
Sys.Application.notifyScriptLoaded();
This sends a notification to the ScriptManager that the
script has been loaded.
Another useful object is the PageRequestManager. This
object manages partial-page postback lifecycle, arising from the use of
UpdatePanels that occur within the page. To get an instance of the
PageRequestManager, simply call the static getInstance() method; do not
construct an instance yourself:
Listing 3
var pageManager = Sys.WebForms.PageRequestManager.getInstance();
When a partial-page update triggers, the page manager fires
a beginRequest method that you can tap into. As a simple illustration, I used
the following code:
Listing 4
<asp:UpdatePanel ID="upTest" runat="server">
<ContentTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%= DateTime.Now.ToString() %>' />
<asp:Button ID="btnUpdate" runat="server" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
<script language="javascript" type="text/javascript">
pageManager.add_beginRequest(myBeginRequestCallback);
function myBeginRequestCallback()
{
alert('begin request');
}
</script>
Whenever the button was clicked to update, the "begin
request" alert message appears in the web page.