Sys.Application.add_load Problem with AJAX Postbacks

by demtron on Wednesday, October 08, 2008 12:04 PM

One of my current projects requires a Web form with AJAX interaction between a TextBox and a ListBox.  As a user types into the TextBox, the keyup of event triggers and async postback to filter the list of items in the listbox.  I used the Sys.Application.add_load accessor and $addHandler method to accomplish this as follows:

        Sys.Application.add_load(PageLoad);
        Sys.Application.add_unload(PageUnload);
        
        function PageLoad(sender, args)
        {
            var txtbox = $get('<%= SearchBox.ClientID %>');
            $addHandler(txtbox, "keyup", fn_KeyPress);
        }

        function PageUnload(sender, args)
        {
            var txtbox = $get('<%= SearchBox.ClientID %>');
            $removeHandler(txtbox, "keyup", fn_KeyPress);
        }

The problem is that, on each postback, the handlers are added again.  So, on the first keyup event, the handler method is fired once.  On the second keyup event, the handler method is fired twice, and so on.  This is a problem.

The solution to determine, through JavaScript, whether the page is a postback of not.  This can be accomplished by the Sys.ApplicationLoadEventArgs.isPartialLoad property.  I added it int the Load handler method as follows:

        function PageLoad(sender, args)
        {
            if (!args.get_isPartialLoad())
            {
            var txtbox = $get('<%= SearchBox.ClientID %>');
            $addHandler(txtbox, "keyup", fn_KeyPress);
            }
        }

Problem solved!


Comments

2/23/2012 9:24:17 AM #

Pingback from petrkotal.wordpress.com

JavaScript OnLoad event « Petr Kotal's C# blog

petrkotal.wordpress.com

Comments are closed

Powered by BlogEngine.NET 1.5.1.18
Theme by Mads Kristensen · Adapted by Demtron

Bookmark and Share

Calendar

<<  July 2017  >>
MoTuWeThFrSaSu
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar
Log in

Milwaukee SEO Company

Milwaukee Access Programmer/Developer

Milwaukee Website Designer and Developer



Marketing / SEO

Blog Directory
blogarama - the blog directory
TopOfBlogs
Milwaukee area SEO, SEM, ASP.Net