Geoffroy’s Weblog

October 15, 2008

Can you register more than one postback method with an event of the PageRequestManager?

Filed under: AJAX, JavaScript — gseive @ 6:02 pm

 

For the past 10 months I worked on a project using ASP.NET AJAX and the server-centric development model with ScriptManager, UpdatePanel and PageRequestManager.

 

Something I have not read about, but that was actually fairly easy to verify, is “can you register more than one postback method with an event of the PageRequestManager?”.

So I modified the code of a really simple, yet interesting, example that shows how to use the initializeRequest event to allow an asynchronous postback to be canceled. It comes from MSDN at the page for the Sys.WebForms.InitializeRequestEventArgs Class.

Here’s the code I added:

                Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(AddMyMessage);

and

                function AddMyMessage(sender, args)

                {

                    var aspan = $get(messageElement);

                    aspan.innerHTML = aspan.innerHTML + ” it works!”;

                }

 

As you will observe the answer is yes, it can be done.

 

A good read in Partial-Page Rendering Overview is Working with PageRequestManager Events.

 

A more complete example can be found at Canceling an Asynchronous Postback.

 

Some good background information can be found in the section UpdatePanel Control Overview. The AJAX Server Controls section it is a part of also introduces the ScriptManager control, the Timer control and the UpdateProgress control.

 

 

Unfortunately some examples are not coded properly.

 

 

Another good source of information is the book “ASP.NET AJAX in Action”. I found it informative and well organized, covering very well all the different aspects of ASP.NET AJAX.

ASP.NET AJAX in Action

  

Hope that helps.

February 26, 2008

JavaScript and ASP.NET User Controls

Filed under: AJAX, JavaScript, User Control — gseive @ 3:22 am

This is something that I encountered recently on my current project and it bugged me for a little while. So I decided to capture my experience and share it.

Two radio buttons control the enabling or disabling of entry fields. One radio button controls just one entry field (and it’s associated label) and the other one controls a group of four entry fields (with their associated labels). Those four entry fields have been bundled together as a user control. Let’s say all this is in a panel that’s enabled as a modal popup window with the help of an ASP.NET AJAX modal popup extender. You want to write some JavaScript to handle the behavior, maybe thinking that you can enable / disable the entry fields as a group by getting their container (the user control), retrieving them (with a getElementsByTagName ) and iterating over them.
Even though you can get a clientid for a user control, no html is generated for it. So using that id on the JavaScript side yields a null reference. You have to provide your own container. In my case the layout was organized using a table, so each set of controls was in its own cell that had already been assigned an id for css purposes. Once that was figured out the rest was easy.

January 30, 2008

ASP.NET AJAX with .NET 2.0 and VisualStudio 2005

Filed under: AJAX, VisualStudio 2005 — gseive @ 4:17 am

Working on a new project for a client, I had to install the ASP.NET AJAX framework on .NET 2.0 / VisualStudio 2005.

I simply followed the instructions from the Appendix A of “ASP.NET AJAX in Action”. The only real difference is that on ajax.asp.net the version for .NET 2.0 is now at the bottom of the page. It isn’t the hottest stuff anymore! Indeed, why bother with all those installs when you can get everything and more by installing VisualStudio 2008 (based on .NET 3.5)!

Probably the most exciting part is the AJAX Control Toolkit. The list of controls is simply amazing and all those controls should give you a huge jump start on any project.

November 29, 2007

ASP.NET AJAX (UpdatePanel)

Filed under: AJAX, VSTS 2008 — gseive @ 8:10 pm

I had done some reading on ASP.NET AJAX throughout the year but hadn’t had a chance to apply it. On my current project a perfect opportunity presented itself. On one page there is a list of folders the user can choose from. When a folder is selected it displays a list of photos. Each time a different folder is selected this causes a postback and the entire page is refreshed causing that annoying flickering (like on MSDN when browsing methods and properties of a class…). Since the site is designed with a master page and following the advice in “ASP.NET in Action” I added the ScriptManager to the master page and wrapped the controls with an UpdatePanel. And Voilà! Wow effect guaranteed! I knew what to expect, but it was still amazing to watch. Très cool!

When using VSTS 2008 the Web.config file includes all the needed configuration settings. Enhancing an existing ASP.NET 2.0 application requires adding the new settings, detailed in the section Configuring ASP.NET AJAX on ASP.NET – AJAX.

Of course, there is more to ASP.NET AJAX than UpdatePanel, and on that to-do list I have the web method / page method approach to investigate.

Some of the articles I read on the topic (in MSDN Magazine):

There’s always something to learn from Jeff Prosise and Dino Esposito!

Blog at WordPress.com.