2

I'm trying to use an UpdatePanel in my ASP.NET application. Unfortunately, it seems that I can't do this if I am using Server.Transfer() in my application.

Modifying that component of the application is not possible - the architecture makes extensive use of Server.Transfer() - in essence, every page request goes through this method. Does any workaround exist for this issue exist? Having to do full-page postbacks is so unfashionable these days...

Sander
  • 25,685
  • 3
  • 53
  • 85

3 Answers3

4

I've got it! Thank Og for strange foreign language blogs :)

To fix it, I can simply tell the ASP.NET AJAX client-side framework to direct the partial request directly at the real target of the Server.Transfer() call. I am quite scared of the possible side-effects (who knows what this skips - the infrastructure does have a purpose) but it seems to be working fine so far.

Here is the method that fixes the problem, called in my page's Load event:

    ///
    /// Adds to the page a JavaScript that corrects the misbehavior of AJAX when a page is target of a Server.Transfer call.
    ///
    protected void AjaxUrlBugCorrection()
    {
        string actualFile = Server.MapPath(AppRelativeVirtualPath);
        string redirectFile = Server.MapPath(Context.Request.FilePath);
        string baseSiteVirtualPath = HttpRuntime.AppDomainAppVirtualPath;

        if (actualFile != redirectFile)
        {
            System.Text.StringBuilder sbJS = new System.Text.StringBuilder();
            string actionUrl = string.Format("'{0}'", baseSiteVirtualPath + AppRelativeVirtualPath.Replace("~", String.Empty));
            sbJS.Append("Sys.Application.add_load(function(){");
            sbJS.Append(" var form = Sys.WebForms.PageRequestManager.getInstance()._form;");
            sbJS.Append(" form._initialAction = " + actionUrl + ";");
            sbJS.Append(" form.action = " + actionUrl + ";");
            sbJS.Append("});");
            ClientScript.RegisterStartupScript(this.GetType(), "CorrecaoAjax", sbJS.ToString(), true);
        }
    }
Sander
  • 25,685
  • 3
  • 53
  • 85
  • There are no aspx changes involved - just call this method in Page_Load and all UpdatePanels on the page will start working. – Sander Jan 07 '10 at 11:36
0

This should work in a more proper way:

if you call Server.Transfer from a control's event handler just register that control as a PostBackTrigger in the Triggers section of the update panel:

<Triggers>
    <asp:PostBackTrigger ControlID="controlId" />
</Triggers>
Vladislav
  • 2,772
  • 1
  • 23
  • 42
0

Response.Write("window.open('new tab page link','_blank');"); Response.Write("this.window.location='link to another page';");

Amit Gohel
  • 17
  • 3
  • Welcome to SO. Please provide some context to your answer. Link-only and code-only answers are not conform to the standards of this site: http://stackoverflow.com/help/how-to-answer – Uwe Allner Aug 15 '16 at 11:08
  • i have pop pup on my listing page so when i used Server.Transfer to go my popup page. it was not redirect properly. as the popup shows on load and link not changed on location bar. thats weird so i used this code to fix the problem and its working fine. – Amit Gohel Aug 15 '16 at 11:10