views:

487

answers:

3

I have a page with several user controls that use Ajax. When I try to do a response.redirect from this page, Ajax traps the call and does not allow the redirect.

How can I do a response.redirect from an Ajax page?

Thanks...

A: 

How much control over the AJAX actions do you have in the user controls? If you can modify the client side, the easiest thing to do is to return a JSON object that you can parse and send the URL that you want to redirect to as data. Once you get the data on the client, simply set window.location.href to the url.

With jQuery it would look something like:

$.ajax({
   url: '/some/url',
   dataType: 'json',
   type: 'post',
   data: $('form').serialize(),
   success: function(data) {
       if (data.Redirect) {
          window.location.href = data.Redirect;
       }
       else {
         ...handle other responses...
       }
   }
   ... more options...
});
tvanfosson
All my code is in C#, ASP.NET 2.0. I am not familiar with JSON.
Bob Jones
Check out this blog article on creating JSON with the JavaScriptSerializer: http://blogs.msdn.com/rakkimk/archive/2009/01/30/asp-net-json-serialization-and-deserialization.aspx. You just need to define the object that you need to send back and then use Response.Write on the serialized version.
tvanfosson
A: 

as I stated in the other question:

add this to your web.config

<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
Russ Bradberry
That code is already in my Web.config
Bob Jones
A: 

Here is blog post that will help you to resolve the issue.

http://patelshailesh.com/index.php/response-redirect-throws-an-error-when-called-inside-update-panel

shailesh