无法使用跨页面过帐取消注册UpdatePanel例外

时间:2013-03-21 18:47:32

标签: c# asp.net cross-page-posting

我遇到了可怕的异常“在尝试组合弹出窗口和某些跨页面发布时,无法取消注册带有ID'UpdatePanel'的UpdatePanel,因为它没有在ScriptManager中注册”。我已经用一个小例子重现了这个问题。

我已经看到了处理UpdatePanel的Unload事件的其他问题,但这似乎不适用于添加跨页面发布。

如何解决此问题?

使用以下代码复制问题:

  1. 启动设置页面
  2. 按查看弹出链接
  3. 关闭弹出窗口
  4. 按“下一步”按钮转移到“目标”页面
  5. 按“返回”按钮转移到“设置”页面
  6. 按查看链接并关闭弹出窗口
  7. 按“下一步”按钮,查看失败
  8. 以下是重点(原谅长度,但它应该是可以切割的):

    Setup.aspx

    <head runat="server">
        <title>Setup </title>
        <script type="text/javascript">
            function openCenteredWindow(url, height, width, name, parms) {
                //Snip setting up window location stuff
                var win = window.open(url, name, winParms);
                return win;
            }
        </script>
    </head>
    <body>    
        <h1>Setup Page</h1>
        <form id="aspnetForm" runat="server">
        <div>
            <asp:TextBox ID="txtData" runat="server" Width="80%" Text="<%# Information %>" />
            <br />
    
            <asp:LinkButton ID="lbViewData" runat="server" 
            OnClientClick="aspnetForm.target='ViewDataPopup';" 
            Text="View In Popup" /> <br />
    
            <asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" />
        </div>
        <div>
            <br />
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        <asp:updatepanel ID="UpdatePanel" runat="server" OnUnload="UpdatePanel_Unload"></asp:updatepanel>
        </div>
        </form>
    </body>
    

    Setup.Aspx.cs

    public partial class Setup : System.Web.UI.Page
    {
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (this.ViewState["Information"] != null)
                _Information = this.ViewState["Information"].ToString();
        }
    
        protected override object SaveViewState()
        {
            this.ViewState["Information"] = _Information;
            return base.SaveViewState();
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage != null && PreviousPage is TargetPage)
            {
                _Information = ((TargetPage)PreviousPage).SetupData;
            }
            else if (!Page.IsPostBack)
            {
                _Information = String.Format("This test started at {0}", DateTime.Now);
            }
            Page.DataBind();
            lbViewData.PostBackUrl = "ViewData.aspx?u=0";
            lbViewData.Attributes.Add("onclick", "JavaScript:openCenteredWindow(\"ViewData.aspx?u=0\", 400, 300, \"ViewDataPopup\", 'scrollbars=yes');");
        }
    
        private string _Information;
        public string Information
        {
            get { return _Information; }
        }
    
        protected void btnNext_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Server.Transfer("~/TargetPage.aspx");
        }
    }
    

    ViewData.aspx

    <%@ PreviousPageType VirtualPath="~/Setup.aspx" %> 
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <head runat="server">
        <title>View Data</title>
           <script type="text/javascript">
               function fixForm() {
                   opener.document.getElementById("aspnetForm").target = "";
                   opener.document.getElementById("aspnetForm").action = "";               
               }
        </script>
    </head>
    <body onload="fixForm()">
        <form id="aspnetForm" runat="server">
        <div>
            <h2 >View Data</h2>
        </div>
        <asp:Label ID="lblData" runat="server" Text="<%# SetupData %>" />
        </form>
    </body>
    

    ViewData.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            Setup setup = (Setup)PreviousPage;
            this.SetupData = setup.Information ?? "Data not set in Setup Page";
        }
        Page.DataBind();
    }
    
    private string _setupData = "Did not get updated data";
    protected string SetupData
    {
        get { return _setupData; }
        set { _setupData = value; }
    }
    

    TargetPage.aspx

    <%@ PreviousPageType VirtualPath="~/Setup.aspx" %>
    <body style="background-color: #9999BB">
        <h1>Target Page</h1>
        <form id="aspnetForm" runat="server">
        <div>
            <asp:Label ID="lblData" runat="server" Text="<%# SetupData %>" /><br />
            <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
        </div>
        </form>
    </body>
    

    TargetPage.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.PreviousPage == null)
                    Response.Redirect("Setup.aspx");
    
                this.SetupData = PreviousPage.Information;
            }
            Page.DataBind();
        }
    
        public string SetupData
        {
            get { return ViewState["SetupData"].ToString(); }
            set { ViewState["SetupData"] = value; }
        }
    
        protected void btnBack_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Server.Transfer("~/Setup.aspx");
        }
    

    正如您所看到的,只需将UpdatePanel放在页面上就可以发生此错误。

0 个答案:

没有答案