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

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

User Comments

Title: Good Article   
Name: Amol
Date: 2010-09-17 2:48:44 AM
Comment:
Very good article
Title: Informative   
Name: PickaTutorial.com
Date: 2010-08-07 2:44:15 AM
Comment:
A good informative resource.
Title: good   
Name: good
Date: 2010-07-23 2:53:53 AM
Comment:
good
Title: Good   
Name: Ananth
Date: 2009-04-15 12:51:48 PM
Comment:
Good
Title: Parameters to be passed webserivices (reply)   
Name: Brian Mains
Date: 2009-02-09 8:59:45 AM
Comment:
Hello,

You could pass the values as straight parameters; I preferrably like to have the web service use a custom object that has those parameters. The reason is that it makes maintenance easier. If you need 21 parameters later, you have to change the web service, and anything that connects to this web service.

This way, you simply add a parameter to the class and make use of it in the web service, less coding work to do this way.

The proxy generator will also generate the class that's accepted by the parameter, as long as it's simple enough (not deeply nested properties), and you can instantiate this and pass it to the web service call.
Title: Parameters to be passed webserivices.   
Name: Rupesh Kumar Tiwari
Date: 2009-02-09 5:37:28 AM
Comment:
Hi,
I have to pass 20 parameters to a webmethod from the clientside.so, is it advisable to make a webmethod that will take 20 parameters or else i have to send one array of parameters to reduce the confusion. Any other suggesion do you have. then kindly tell me. Any quick response is appreciable.

Thanks,
Title: Thanks a lot for the info   
Name: The Black Knight
Date: 2008-11-19 6:24:52 PM
Comment:
Excellent info
Title: Nice Article   
Name: ---
Date: 2008-06-21 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: 2008-06-12 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: 2008-05-02 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: 2008-04-30 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: 2008-04-23 9:02:58 AM
Comment:
its very new concept 4 me. thanks
Title: very good   
Name: panakj lahjoti
Date: 2008-04-21 7:34:42 AM
Comment:
hai
i found this aticle very useful for the newbies
Title: Very helpful   
Name: Raviraj
Date: 2008-04-20 1:28:30 PM
Comment:
thanks it is very helpful to me






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 4:13:05 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search