internal void RegisterDispose(Control control, string disposeScript) {
            if (control == null) {
                throw new ArgumentNullException("control");
            }
            if (control.Page == null) {
                throw new ArgumentException(AtlasWeb.ScriptRegistrationManager_ControlNotOnPage, "control");
            }
            if (disposeScript == null) {
                throw new ArgumentNullException("disposeScript");
            }

            // Locate the parent UpdatePanel of the control
            Control parent = control.Parent;
            UpdatePanel parentUpdatePanel = null;
            while (parent != null) {
                parentUpdatePanel = parent as UpdatePanel;
                if (parentUpdatePanel != null) {
                    break;
                }
                parent = parent.Parent;
            }
            if (parentUpdatePanel != null) {
                // During async posts we build up a list of ScriptDisposes. Later
                // we go through the list and filter out ones that aren't inside
                // UpdatePanels that are refreshing.
                // DevDiv Bugs 128123: Build the list on non-async postbacks as well,
                // so that GetRegisteredDisposeScripts returns them.
                RegisteredDisposeScript entry = new RegisteredDisposeScript(control, disposeScript, parentUpdatePanel);
                ScriptDisposes.Add(entry);

                if (!_scriptManager.IsInAsyncPostBack) {
                    // During non-async requests we register script immediately to do the
                    // dispose. This is necessary because some controls will register as late
                    // as Render(), at which point it would be too late to build up a list
                    // for processing later.
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    // 256 seems like a nice number so that we don't have to resize the StringBuilder except in very rare cases
                    StringBuilder sb = new StringBuilder(256);
                    sb.Append("Sys.WebForms.PageRequestManager.getInstance()._registerDisposeScript(");
                    serializer.Serialize(parentUpdatePanel.ClientID, sb);
                    sb.Append(", ");
                    serializer.Serialize(disposeScript, sb);
                    sb.AppendLine(");");

                    // DevDiv Bugs 128123: Register directly with ClientScriptManager so that a RegisteredScript
                    // entry is not created. Otherwise, calls to RegisterDispose would result in viewable
                    // RegisteredScript entries through GetRegisteredStartupScripts().
                    _scriptManager.IPage.ClientScript.RegisterStartupScript(typeof(ScriptRegistrationManager),
                        _scriptManager.CreateUniqueScriptKey(),
                        sb.ToString(),
                        true);
                }
            }
        }
        internal void RegisterDispose(Control control, string disposeScript)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            if (control.Page == null)
            {
                throw new ArgumentException(AtlasWeb.ScriptRegistrationManager_ControlNotOnPage, "control");
            }
            if (disposeScript == null)
            {
                throw new ArgumentNullException("disposeScript");
            }

            // Locate the parent UpdatePanel of the control
            Control     parent            = control.Parent;
            UpdatePanel parentUpdatePanel = null;

            while (parent != null)
            {
                parentUpdatePanel = parent as UpdatePanel;
                if (parentUpdatePanel != null)
                {
                    break;
                }
                parent = parent.Parent;
            }
            if (parentUpdatePanel != null)
            {
                // During async posts we build up a list of ScriptDisposes. Later
                // we go through the list and filter out ones that aren't inside
                // UpdatePanels that are refreshing.
                // DevDiv Bugs 128123: Build the list on non-async postbacks as well,
                // so that GetRegisteredDisposeScripts returns them.
                RegisteredDisposeScript entry = new RegisteredDisposeScript(control, disposeScript, parentUpdatePanel);
                ScriptDisposes.Add(entry);

                if (!_scriptManager.IsInAsyncPostBack)
                {
                    // During non-async requests we register script immediately to do the
                    // dispose. This is necessary because some controls will register as late
                    // as Render(), at which point it would be too late to build up a list
                    // for processing later.
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    // 256 seems like a nice number so that we don't have to resize the StringBuilder except in very rare cases
                    StringBuilder sb = new StringBuilder(256);
                    sb.Append("Sys.WebForms.PageRequestManager.getInstance()._registerDisposeScript(");
                    serializer.Serialize(parentUpdatePanel.ClientID, sb);
                    sb.Append(", ");
                    serializer.Serialize(disposeScript, sb);
                    sb.AppendLine(");");

                    // DevDiv Bugs 128123: Register directly with ClientScriptManager so that a RegisteredScript
                    // entry is not created. Otherwise, calls to RegisterDispose would result in viewable
                    // RegisteredScript entries through GetRegisteredStartupScripts().
                    _scriptManager.IPage.ClientScript.RegisterStartupScript(typeof(ScriptRegistrationManager),
                                                                            _scriptManager.CreateUniqueScriptKey(),
                                                                            sb.ToString(),
                                                                            true);
                }
            }
        }