For several days now I’ve been messing around with Ajax calls using different combinations of javascript, ASP controls, and regular controls. I’ve gotten a bit of understanding about what’s going on, but using ASP controls still hides too much of the machinery, and I want to have a deeper understanding. With that aim, can anyone tell me why the following setup doesn’t quite work?
I have a file “Testy.aspx” with the following:
<asp:Content>
<script type="text/javascript">
// a standard home-grown Ajax javascript method
function ajaxfunction() {
var ajaxObj = getAjaxObj(); // does the usual browser-detection
if (ajaxObj) {
ajaxObj.open("GET", "Testy.aspx", true);
ajaxObj.setRequestHeader("IsAjaxRequest", "true");
ajaxObj.send();
ajaxObj.onreadystatechange = function() {
if (ajaxObj.readyState == 4) {
document.getElementById("testytext").appendChild(document.createTextNode(ajaxObj.responseText));
}
}
}
}
</script>
... other unrelated html, ASP controls, etc...
<input id="testybutton" type="button" value="baroo" onclick="ajaxfunction()" />
<div id="testytext"></div>
</asp:Content>
Meanwhile, I have a code-behind function “Testy.aspx.vb” with the following:
Partial Public Class Testy
Inherits System.Web.UI.Page
Implements System.Web.IHttpHandler
...code for an ordinary (non-Ajax) request is in the middle here...
' Now I have code for Ajax requests
Overrides Sub ProcessRequest(ByVal context As HttpContext)
If context.Request.Headers("IsAjaxRequest") = "true" Then
context.Response.ContentType = "text/plain"
context.Response.Write("Hello World!")
Else
MyBase.ProcessRequest(context)
End If
End Sub
Overloads ReadOnly Property IsReusable() As Boolean
Get
Return False
End Get
End Property
End Class
So, I hoped to make my .aspx file do double-duty both as the regular-page request handler as well as the Ajax request handler. However, when I click the button (“baroo”) to generate the Ajax request, the result that ends up written back to the “testytext” div is the raw html for the entire page, as if under normal request conditions. Clearly, my attempt to override the page request by making the code-behind implement IHttpHandler and supplying an “Overrides Sub ProcessRequest” method is not working. The server is still treating the Ajax request as a normal request, and in fact my own “ProcessRequest” method is never even called.
Is it possible to build a page/handler like this? How can I intercept the incoming request from the client and respond accordingly? This is how Ajax works, right? So it must be possible.
Again, I’m deliberately doing this as an excercise to avoid the use of “magic” ASP controls like UpdatePanels, so please don’t advise their use.
Setup a different file that is your generic handler and don’t combine the two.
Generic Handlers (.ashx) and regular web forms (.aspx) differ in that generic handlers do NOT run all of the normal win form page processing and instead simplify things quite a bit. Generally speaking you don’t want the full page model for this situation, hence the reason they should be different files.
If you really want to have the methods in your main page that you’ll call via ajax, then look into the “WebMethod” attribute. However, I wouldn’t go this route.