Is there any workaround for the UpdatePanel + Server.Transfer problem?

孤人 提交于 2019-12-06 12:43:32

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);
        }
    }

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!