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.


Centering a DIV Window with Cross-Browser JavaScript

by demtron on Wednesday, January 14, 2009 02:28 PM

Years ago, I wrote a little bit of JavaScript to position a DIV control in the center of a browser window space by calculating the browser area, the window size, then determine the coordinates of the place where the DIV would need to be placed.  Since I forgot where I put it and it brobably wouldn't be cross-browser compatible anyway (orginally written for IE 5/6), I searched the web and ran across this JavaScript centering code that accomplishes exactly that.  Here are the excerpts of the code needed to do this.


window.size = function()
{
var w = 0;
var h = 0;
//IE
if(!window.innerWidth)
{
//strict mode
if(!(document.documentElement.clientWidth == 0))
{
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
}
//quirks mode
else
{
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
//w3c
else
{
w = window.innerWidth;
h = window.innerHeight;
}
return {width:w,height:h};
}
window.center = function()
{
var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
var _x = 0;
var _y = 0;
var offsetX = 0;
var offsetY = 0;
//IE
if(!window.pageYOffset)
{
//strict mode
if(!(document.documentElement.scrollTop == 0))
{
offsetY = document.documentElement.scrollTop;
offsetX = document.documentElement.scrollLeft;
}
//quirks mode
else
{
offsetY = document.body.scrollTop;
offsetX = document.body.scrollLeft;
}
}
//w3c
else
{
offsetX = window.pageXOffset;
offsetY = window.pageYOffset;
}
_x = ((this.size().width-hWnd.width)/2)+offsetX;
_y = ((this.size().height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
}

Here's some example code to show how it's supposed to be used:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="Window.Size.js"></script>
<script type="text/javascript">
function showCenter(point)
{
var div = document.createElement("div");
div.style.background = "#dedede";
div.style.position = "absolute";
div.style.top = point.y + "px";
div.style.left = point.x + "px";
div.style.width = "100px";
div.style.height = "100px";
document.body.appendChild(div);
}
</script>
</head>
<body>
<div style="height:1200px"></div>
<input type="button" value="Get Center" onclick="showCenter(window.center({width:100,height:100}))"/>
</body>
</html>

 


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

<<  March 2024  >>
MoTuWeThFrSaSu
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

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