【问题标题】:Is there any workaround for the UpdatePanel + Server.Transfer problem?UpdatePanel + Server.Transfer 问题是否有任何解决方法?
【发布时间】:2009-12-05 12:09:39
【问题描述】:

我正在尝试在我的 ASP.NET 应用程序中使用 UpdatePanel。不幸的是,我似乎无法做到这一点if I am using Server.Transfer() in my application

修改应用程序的那个组件是不可能的——架构广泛使用 Server.Transfer()——本质上,每个页面请求都通过这个方法。是否存在针对此问题的任何解决方法?如今,不得不进行整页回发是如此不时尚......

【问题讨论】:

    标签: asp.net ajax updatepanel server.transfer asynchronous-postback


    【解决方案1】:

    我明白了!感谢 Og strange foreign language blogs :)

    要修复它,我可以简单地告诉 ASP.NET AJAX 客户端框架将部分请求直接指向 Server.Transfer() 调用的实际目标。我很害怕可能的副作用(谁知道这会跳过什么 - 基础设施确实有目的),但到目前为止它似乎运行良好。

    这是解决问题的方法,在我的页面的 Load 事件中调用:

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

    【讨论】:

    • 不涉及 aspx 更改 - 只需在 Page_Load 中调用此方法,页面上的所有 UpdatePanel 都会开始工作。
    【解决方案2】:

    这应该以更合适的方式工作:

    如果您从控件的事件处理程序调用Server.Transfer,只需将该控件注册为更新面板的触发器部分中的 PostBackTrigger:

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

    【讨论】:

      【解决方案3】:

      Response.Write("window.open('新标签页链接','_blank');"); Response.Write("this.window.location='链接到另一个页面';");

      【讨论】:

      • 欢迎来到 SO。请为您的答案提供一些背景信息。仅链接和仅代码的答案不符合本站标准:stackoverflow.com/help/how-to-answer
      • 我的列表页面上有弹出小狗,所以当我使用 Server.Transfer 转到我的弹出页面时。它没有正确重定向。加载时弹出窗口显示,并且位置栏上的链接未更改。这很奇怪,所以我使用这段代码来解决问题并使其正常工作。
      猜你喜欢
      • 2021-12-30
      • 2018-03-20
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多