views:

87

answers:

4

This might be really hacky, but I'm beginning to get desperate

Is it possible to prevent a request from actually hitting the actual page...ie, can I write something in Application_BeginRequest that processes the part that I want, and then skips the rest of the lifecycle?

Oh, and is it possible to do it in such a way that the ajax update panel does throw a fit (return some default content that says, "I haven't done anything sorry")

Ok, mad question over.

+1  A: 

Consider writing an HTTPModule sitting on the front of the request processing flow to take care of that.

twk
This could work - how could it prevent the request from going any further past the HTTPModule?
Paul
@IP - Response.CompleteRequest will stop the request.
David
A: 

Sure, just call Response.End():

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.Url.AbsolutePath.Equals("/PageYouWantToSuppress.aspx", StringComparison.OrdinalIgnoreCase))
    {
     // Do your stuff

     Response.End();
    }
}

ETA: I'm not sure about the UpdatePanel part of your question.

Jon Sagara
Yeah, this one I've tried - it seems to work but doesn't return what the update panel needs. Anyone know what an update panel needs to be happy?
Paul
+2  A: 

You can prevent portions of your code from executing during a partial-page update like this:

if (! ScriptManager.IsInAsyncPostBack) {
    // Code in here won't happen during UpdatePanel actions
}

UpdatePanels execute most of the ASP.NET page lifecycle by design. If you abort that process (via Response.End() for example), the request doesn't complete, so the client won't get the data it needs to update the page's HTML.

When you trigger a server-side method in an UpdatePanel ASP.NET does the following:

  1. Rebuilds the portion of the page within the panel from scratch by running through the normal ASP.NET page lifecycle (well, slightly abridged, but close).
  2. Sends the panel's HTML back to the client.
  3. Rebuilds the panel on the client using javascript DOM manipulation.

(For more see my recent answer to this question, and see UpdatePanel Control Overview or this article for details)

There are several alternatives to the UpdatePanel AJAX implementation that don't involve going through the whole page lifecycle. One of the most popular is Using jQuery to directly call ASP.NET AJAX page methods.

Using jQuery for AJAX calls gets you a little closer to the metal (so to speak) than UpdatePanels, but there's tons of good help to get you started - on StackOverflow itself and on Encosia (the source of that last link).

Jeff Sternal