AJAX Client Side Programming
page 2 of 8
by Brian Mains
Feedback
Average Rating: 
Views (Total / Last 10 Days): 5444/ 350

Application Namespaces/Objects/Lifecycle

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.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 6 and 2 and type the answer here:

User Comments

Title: Nice Article   
Name: ---
Date: 6/21/2008 5:40:01 AM
Comment:
really nice article, it is good for the beginners.
good job :)
Title: dragpanel extender in ajax controls   
Name: lalitha
Date: 6/12/2008 5:46:21 PM
Comment:
its not working as i had written as it was mentioned here in the tutorial plse give a brief coding about that control
Title: expect more   
Name: sivaram
Date: 5/2/2008 4:50:01 AM
Comment:
ok .i expected more.If possible plz explain with the examples
Title: nice but shud little brief   
Name: snehal
Date: 4/30/2008 3:20:58 PM
Comment:
nice article but give more details on ajax its merits... funnalities future implementation ....etc thnx
Title: thanks   
Name: suhas kudekar
Date: 4/23/2008 9:02:58 AM
Comment:
its very new concept 4 me. thanks
Title: very good   
Name: panakj lahjoti
Date: 4/21/2008 7:34:42 AM
Comment:
hai
i found this aticle very useful for the newbies
Title: Very helpful   
Name: Raviraj
Date: 4/20/2008 1:28:30 PM
Comment:
thanks it is very helpful to me






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/5/2008 7:59:24 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search