Detect AJAX postback in Global.asax

by demtron on Friday, January 16, 2009 08:19 AM

I was recently working on a page hit logging scenario that, for reasons having to do with URL rewriting, needed to have the logging code executed in the Application.BeginRequest event of Global.ajax.  The application relies heavily on ASP.Net Ajax, and I needed a was to ferret out the AJAX requests and not log them.  Ordinarily, in the page lifecycle, this would be accomplished by using Page.IsAsync.  In this case, I needed to perform the logging prior to the page code being invoked.

To determine the whether the request was generated by AJAX, I used the following code:

VB.Net

Request.Headers("X-MicrosoftAjax") = "Delta=true"

C#

Request.Headers["X-MicrosoftAjax"] = "Delta=true";

Hope that helps someone out there who has struggled to find an easy answer.


Control Focus, AJAX UpdatePanel Postback and Cursor Position

by demtron on Thursday, December 04, 2008 05:19 PM

When using ASP.Net AJAX to perform a postback from within a textbox input control, I found that the textbox loses focus after the postback.  To overcome this, I added the following code:

ScriptManager.GetCurrent(Me.Page).SetFocus(MyTextBox)

However, upon setting focus back to the textbox, the cursor lands at the beginning of the text in the control.  To solve this, just add the following JavaScript on the page or within window.setTimeout:

var textInput = $get('MyTextBox');
if (textInput.createTextRange)
{
    var range = textInput.createTextRange();
        range.moveStart('character', (textInput.value.length));
        range.collapse();
        range.select();
    }

Browser Detection for ASP.Net Ajax

by demtron on Tuesday, November 04, 2008 10:17 AM

Here's a class that I found in client-side Microsoft AJAX library for browser detection.  It's Sys.Browser.Agent.  For a while, it wasn't documenated anywhere in the Microsoft on-line documentation, but it must have just appeared recently at:

http://msdn.microsoft.com/en-us/library/cc679064.aspx

One example of how this class can be used:

if (Sys.Browser.agent == Sys.Browser.InternetExplorer) 
    { 
    alert("Display this message for IE visitors");
    } 

For Sys.Browser.agent, the four values it can return are:

  • Sys.Browser.InternetExplorer
  • Sys.Browser.Firefox
  • Sys.Browser.Safari
  • Sys.Browser.Opera

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!


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

Bookmark and Share

Calendar

<<  April 2024  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

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