Click here to Skip to main content
6,629,377 members and growing! (21,087 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » General     Advanced License: The Code Project Open License (CPOL)

JavaScript Access to Page Controls after Ajax Update (via Update Panel)

By Sandeep Mewara

Having access to a page via JavaScript once the update panel's update on server gets over
C#, Javascript, CSS, HTML, XHTML.NET 2.0, .NET 3.0, .NET 3.5, ASP, ASP.NET, Ajax, Dev
Posted:22 Sep 2008
Views:17,022
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
53 votes for this article.
Popularity: 6.24 Rating: 3.62 out of 5
16 votes, 30.2%
1
1 vote, 1.9%
2
2 votes, 3.8%
3
3 votes, 5.7%
4
31 votes, 58.5%
5

Introduction

Several times, we are in a situation where we need to do some work the moment the update panel finishes its update (ASP.NET 2.0 AJAX). There are situations like showing an error if any during the update, showing a success message after a proper update, setting up scroll bars, changing some UI component, updating some other part of UI, etc. The list can get longer as per the developer's requirement. I had tried to show how we can get control to the page once the update is finished. We can tweak things via JavaScript the way we need to out there in the handler.

Background  

Earlier I too faced lots of problem and was unable to work out of how to handle anything JUST after an update of update panel. I found lots of my colleagues facing the same issue. They wanted to have a certain UI work like scroll or to trap an error after the update. I found there was no article on The Code Project for the same. It was a real difficult time for my friends to find out the way to handle it. So I planned to write up a demo as soon I had some time. 

Using the Code 

The thing that we are going to use is "adding an end-request handler to the request-manager".

We add the handler to the request manager during page load via JavaScript.

Code example:

function LoadAfterAjaxHandler() 
{
    //Handler attached to the request
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

Now, after having this piece of code in place, whenever the update panel finishes its update, the request would go through the EndRequestHandler.

EndRequestHandler needs to be placed now.  In the handler, one can write whatever requirement one asks for. Errors can be trapped, scrolls can be set, any UI component can be updated.

Code example:

function EndRequestHandler(sender, eArgs) 
{            
   /*
   1. Alert/Message if any after update
   2. Error handling if any
   3. Scroll position set again
   4. Focus/UI update if any
   */
   //Example
   if (eArgs.get_error() == null)
   {
      // Do all the stuffs needed after update through JavaScript here. 
      //1. Alert / Show window / Show Dialog
      //alert("Update was successful");
      //2. Page Controls accessed via JavaScript
      document.getElementById("lblTest").innerText = "Update Successful";
      //3. Page scrolls can be set if needed
      //Set Scroll Position
   }
   else
   {
      // There was an error in the update panels update 
      // caught here and message displayed via JavaScript               
      document.getElementById("lblTest").innerText = 
              "There was an error in update:"+eArgs.get_error().message;
   }
} 

As it can be seen, in the handler one can do all the possible things to a page via JavaScript. I have attached a sample codebase for the same.

Points of Interest

Working with AJAX is fun and it gets more interesting when we can have solutions to all possible scenarios while using it. For example, having a page history while using AJAX was earlier a problem, but lately I came to know that in .NET 3.5, Microsoft has provided AJAX calls history too! That's interesting, isn't it? 

History

  • 23rd September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sandeep Mewara


Member
Did my B.Tech from IIT Kharagpur.

Currently, working as a Module Lead at
Proteans Software Solutions Pvt. Ltd., Bangalore.

Proteans (a CAMO group company) is an outsourcing company
focusing on software product development and business
application development on Microsoft Technology Platform.

=====================================================
Occupation: Web Developer
Company: Proteans Software Solutions Pvt. Ltd., Bangalore.
Location: India India

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Generalthanks Pinmembergaetanocv10:08 6 May '09  
AnswerRe: thanks PinmemberSandeep Mewara20:13 9 Jun '09  
QuestionNice article Pinmembershalan9911:55 12 Nov '08  
AnswerRe: Nice article [modified] PinmemberSandeep Mewara19:32 26 Nov '08  
GeneralRe: Nice article Pinmemberjohram1:23 23 Mar '09  
GeneralGood article Pinmemberachuki10:24 20 Oct '08  
GeneralRe: Good article PinmemberSandeep Mewara2:20 20 Oct '08  
GeneralGood work! Pinmemberchkumar7:37 14 Oct '08  
AnswerRe: Good work! PinmemberSandeep Mewara3:51 15 Oct '08  
GeneralNice! PinmemberAki829:27 11 Oct '08  
AnswerRe: Nice! PinmemberSandeep Mewara21:27 11 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Sep 2008
Editor: Deeksha Shenoy
Copyright 2008 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project