1

I am getting the dreaded exception "Cannot unregister UpdatePanel with ID 'UpdatePanel' since it was not registered with the ScriptManager" when trying to combine a popup window and some Cross-Page Posting. I've been able to reproduce the problem with a small example.

I have seen other questions that handle the Unload event for the UpdatePanel, but that doesn't seem to work with the addition of the Cross Page Posting.

How do I get around this issue?

To duplicate the issue with the code below:

  1. Start the Setup page
  2. Press the View Popup link
  3. Close the popup
  4. Press the Next button to transfer to the Target page
  5. Press the Back button to transfer to the Setup page
  6. Press the View link and close the popup
  7. Press the Next button, and see the failure

Here are the highlights (forgive the length, but it should be cut-and-pastable):

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

As you can see, it's enough to have the UpdatePanel just sitting on the page and doing nothing for this error to occur.

Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92

0 Answers0