The UpdatePanel is one of the coolest feature of Microsoft's ASP.NET AJAX. For some reason if you want to detect an ajax request on the page code-behind it's so easy. Take a look at the code snipped below :
if(ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
//this is an ajax postback
}
On the other hand if you use a custom httpmodule with asp.net ajax web application, you will probably
get this error :
Sys.WebForms.PageRequestManagerParserErrorExeption: The message received from the
server could not be parsed. Common causes for this error are when the response
is modified by calls to Response.Write(), response filters, HttpModules, or server
trace is enabled. Details: ...
In this situation, we can not use ScriptManager's IsInAsyncPostBack property
cause we're working at the httpmodule level. We need something else to catch
ajax request.So I began to look for a special property which represents the ajax
request. Look at the page's request headers :
Cache-Control : no-cache
Connection : Keep-Alive
Content-Length : 720
Content-Type : application/x-www-form-urlencoded
Accept : */*
Accept-Encoding : gzip, deflate
Accept-Language : tr
Cookie : ASP.NET_SessionId=gytqakjnhobwd0utbwd5fo55
Host : localhost:3548
Referer : http://localhost:3548/WebForm1.aspx
User-Agent : Mozilla/4.0 (compatible; MSIE 7.0
x-microsoftajax : Delta=true
UA-CPU : x86
Whoala ! Asp.Net Ajax adds an extra header tag for it's request. Just look for the 'x-microsoftajax' tag on HttpModule's BeginRequest event. If it is existing then it's an ajax request.
if (Request.Headers["x-microsoftajax"] != null)
{
//ajax request
}