简体   繁体   中英

Response.Redirect in .NET 4.0

Response.Redirect() no longer working when upgrade the application to ASP.NET 4.0

  • Response.Redirect() is used inside Update panel
  • and we using the AjaxToolKit 4.0

it gives me the error:

Error: Sys.WebForms.PageRequestManagerParserErrorException: 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: Error parsing near

you have to do this

string redirectURL=(a proper url goes here)

string script = "window.location='" + redirectURL + "';";

ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);

Try passing True as the second argument like this:

Response.Redirect("http://...", true);

Had the same issue... You need to replace your version of the AjaxControlToolkit with the latest version built specifically for 4.0. It's a drop-in replacement, so it should affect anything else. See Ajaxcontroltoolkit on codeplex

We had the same issue. Resolved by using PostBackTrigger control

<Triggers>
   <asp:PostBackTrigger ControlID="UploadButton" />
</Triggers>

http://msdn.microsoft.com/en-us/library/system.web.ui.postbacktrigger.aspx

I have been choking with this problem for entire days without advancing. Update contents of a panel, AsyncPostBackTrigger if the caller is outside updatePanel. If you want to redirect, you add it as PostBackTrigger. Both things on updatepanel using:

<asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:LinkButton ID="link" runat="server" OnClick="link_Click"/>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="link" />
    </Triggers>
</asp:UpdatePanel>

My issue had to do with the controls being generated dinamically inside a custom control due a listview.

The solution:

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(myRedirectButton);

In my case, i was using a listview on the item data bound, so i got the control using:

Control myRedirectButton = ListViewDataItem.GetControl("controlId")

Keep in mind the diference between Async and not ones. It was the main point that i didnt notice it until very far.

You are trying to redirect to another page with an async request.

You can overload the Response.Redirect function and set it to false.

Response.Redirect("URL",false);

by setting it to false, it will terminate your current request and go to the next request.

I am 100% sure it will work for you.

You should try doing it like this:

Response.Redirect("URL", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();

You will be redirected and no error will be thrown.

This is also happening in one of our projects that we migrated from.Net 3.5 to.Net 4.0

The problem only occurs when compression is enabled . We had a custom Response Filter in the Global.asax.cs file to apply gzip compression to each response when supported by the browser.

The solution was to exclude the addition of that filter when requests were made by AJAX (an update panel). This is easy to distinguish because the request contains a X-MicrosoftAjax: Delta=true header. This can be accomplished by wrapping the logic inside an if:

if (Request.Headers["X-MicrosoftAjax"] != "Delta=true")
{
    // Compression logic here
}

See also: https://forums.asp.net/t/1564697.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM