Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing .aspx page as Ajax event handler?
    primarykey
    data
    text
    <p>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?</p> <p>I have a file "Testy.aspx" with the following:</p> <pre><code>&lt;asp:Content&gt; &lt;script type="text/javascript"&gt; // 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)); } } } } &lt;/script&gt; ... other unrelated html, ASP controls, etc... &lt;input id="testybutton" type="button" value="baroo" onclick="ajaxfunction()" /&gt; &lt;div id="testytext"&gt;&lt;/div&gt; &lt;/asp:Content&gt; </code></pre> <p>Meanwhile, I have a code-behind function "Testy.aspx.vb" with the following:</p> <pre class="lang-vb prettyprint-override"><code>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 </code></pre> <p>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.</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload