public void RenderActiveArrayDeclarations(List <UpdatePanel> updatePanels, HtmlTextWriter writer)
        {
            Debug.Assert(writer != null, "Should always have a writer");
            List <RegisteredArrayDeclaration> entriesToRender = new List <RegisteredArrayDeclaration>();

            // For each entry registered in the page, check and see which ones
            // came from controls within UpdatePanels that are going to be updated.
            Control lastControl = null;

            foreach (RegisteredArrayDeclaration entry in ScriptArrays)
            {
                Control child = entry.Control;
                // if the owning control is the same as the last one that we know was active,
                // no need to check IsControlRegistrationActive
                bool isActive = ((lastControl != null) && (child == lastControl)) ||
                                IsControlRegistrationActive(updatePanels, child, true);

                if (isActive)
                {
                    lastControl = child;
                    if (!entriesToRender.Contains(entry))
                    {
                        entriesToRender.Add(entry);
                    }
                }
            }

            foreach (RegisteredArrayDeclaration activeRegistration in entriesToRender)
            {
                PageRequestManager.EncodeString(writer,
                                                PageRequestManager.ArrayDeclarationToken,
                                                activeRegistration.Name,
                                                activeRegistration.Value);
            }
        }
        public void RenderActiveSubmitStatements(List <UpdatePanel> updatePanels, HtmlTextWriter writer)
        {
            Debug.Assert(writer != null, "Should always have a writer");
            List <RegisteredScript> entriesToRender = new List <RegisteredScript>();
            // no comparer needed because it will contain ScriptKeys which implement Equals
            ListDictionary uniqueEntries = new ListDictionary();

            // For each entry registered in the page, check and see which ones
            // came from controls within UpdatePanels that are going to be updated.
            Control lastControl = null;

            foreach (RegisteredScript entry in ScriptSubmitStatements)
            {
                Control child = entry.Control;

                bool isActive = ((lastControl != null) && (child == lastControl)) ||
                                IsControlRegistrationActive(updatePanels, child, true);

                if (isActive)
                {
                    lastControl = child;
                    ScriptKey scriptKey = new ScriptKey(entry.Type, entry.Key);
                    if (!uniqueEntries.Contains(scriptKey))
                    {
                        entriesToRender.Add(entry);
                        uniqueEntries.Add(scriptKey, entry);
                    }
                }
            }

            foreach (RegisteredScript activeRegistration in entriesToRender)
            {
                PageRequestManager.EncodeString(writer, PageRequestManager.OnSubmitToken, null, activeRegistration.Script);
            }
        }
        public void RenderActiveExpandos(List <UpdatePanel> updatePanels, HtmlTextWriter writer)
        {
            Debug.Assert(writer != null, "Should always have a writer");
            if (updatePanels == null)
            {
                return;
            }
            List <RegisteredExpandoAttribute> entriesToRender = new List <RegisteredExpandoAttribute>();

            // For each entry registered in the page, check and see which ones
            // came from controls within UpdatePanels that are going to be updated.
            Control lastControl = null;

            foreach (RegisteredExpandoAttribute entry in ScriptExpandos)
            {
                Control child = entry.Control;

                bool isActive = ((lastControl != null) && (child == lastControl)) ||
                                IsControlRegistrationActive(updatePanels, child, false);

                if (isActive)
                {
                    lastControl = child;
                    if (!entriesToRender.Contains(entry))
                    {
                        entriesToRender.Add(entry);
                    }
                }
            }

            foreach (RegisteredExpandoAttribute activeRegistration in entriesToRender)
            {
                string propertyReference = "document.getElementById('" +
                                           activeRegistration.ControlId + "')['" + activeRegistration.Name + "']";
                string value;
                if (activeRegistration.Encode)
                {
                    value = "\"" + HttpUtility.JavaScriptStringEncode(activeRegistration.Value) + "\"";
                }
                else if (activeRegistration.Value != null)
                {
                    value = "\"" + activeRegistration.Value + "\"";
                }
                else
                {
                    value = "null";
                }
                PageRequestManager.EncodeString(writer, PageRequestManager.ExpandoToken, propertyReference, value);
            }
        }
예제 #4
0
        protected internal override void RenderChildren(HtmlTextWriter writer)
        {
            if (_asyncPostBackMode)
            {
                Debug.Assert(!DesignMode, "Shouldn't be in DesignMode");
                // Render might sometimes be called twice instead of just once if we are forcing
                // all controls to render to ensure EventValidation is valid.
                if (_rendered)
                {
                    return;
                }

                HtmlTextWriter childWriter = new HtmlTextWriter(new StringWriter(CultureInfo.CurrentCulture));
                base.RenderChildren(childWriter);

                PageRequestManager.EncodeString(writer, UpdatePanelToken, ClientID, childWriter.InnerWriter.ToString());
            }
            else
            {
                Debug.Assert(!_rendered);

                writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
                if (_attributes != null)
                {
                    _attributes.AddAttributes(writer);
                }

                if (RenderMode == UpdatePanelRenderMode.Block)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                }
                else
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                }
                base.RenderChildren(writer);
                writer.RenderEndTag();
            }

            _rendered = true;
        }
        public void RenderActiveScriptDisposes(List <UpdatePanel> updatePanels, HtmlTextWriter writer)
        {
            Debug.Assert(writer != null, "Should always have a writer");
            if (updatePanels == null)
            {
                return;
            }

            // For each entry registered in the page, check and see which ones
            // came from controls within UpdatePanels that are going to be updated.
            foreach (RegisteredDisposeScript entry in ScriptDisposes)
            {
                if (IsControlRegistrationActive(updatePanels, entry.ParentUpdatePanel, false))
                {
                    PageRequestManager.EncodeString(
                        writer,
                        PageRequestManager.ScriptDisposeToken,
                        entry.ParentUpdatePanel.ClientID,
                        entry.Script);
                }
            }
        }
        public void RenderActiveHiddenFields(List <UpdatePanel> updatePanels, HtmlTextWriter writer)
        {
            Debug.Assert(writer != null, "Should always have a writer");
            List <RegisteredHiddenField> entriesToRender = new List <RegisteredHiddenField>();
            ListDictionary uniqueEntries = new ListDictionary(StringComparer.Ordinal);

            // For each entry registered in the page, check and see which ones
            // came from controls within UpdatePanels that are going to be updated.
            Control lastControl = null;

            foreach (RegisteredHiddenField entry in ScriptHiddenFields)
            {
                Control child = entry.Control;

                bool isActive = ((lastControl != null) && (child == lastControl)) ||
                                IsControlRegistrationActive(updatePanels, child, true);

                if (isActive)
                {
                    lastControl = child;
                    if (!uniqueEntries.Contains(entry.Name))
                    {
                        entriesToRender.Add(entry);
                        uniqueEntries.Add(entry.Name, entry);
                    }
                }
            }

            foreach (RegisteredHiddenField activeRegistration in entriesToRender)
            {
                PageRequestManager.EncodeString(writer,
                                                PageRequestManager.HiddenFieldToken,
                                                activeRegistration.Name,
                                                activeRegistration.InitialValue);
            }
        }
        private static void WriteScriptWithTags(HtmlTextWriter writer,
                                                string token,
                                                RegisteredScript activeRegistration)
        {
            // If the content already has script tags, we need to parse out the contents
            // so that the client doesn't have to. The contents may include more than one
            // script tag, but no other content (such as arbitrary HTML).
            string scriptContent = activeRegistration.Script;

            int lastIndex = 0;

            for (Match match = ScriptTagRegex.Match(scriptContent, lastIndex); match.Success; match = ScriptTagRegex.Match(scriptContent, lastIndex))
            {
                CheckScriptTagTweenSpace(activeRegistration, scriptContent, lastIndex, match.Index - lastIndex);

                OrderedDictionary attrs = new OrderedDictionary();

                if (match.Groups["empty"].Captures.Count > 0)
                {
                    // Self-closing tag

                    // No need to do anything since attributes are processed later
                    lastIndex = match.Index + match.Length;
                }
                else
                {
                    // Open tag with explicit close tag

                    // Need to find close tag so that we can locate the inner contents
                    int indexOfEndOfScriptBeginTag = match.Index + match.Length;
                    int indexOfScriptEndTag        = scriptContent.IndexOf("</script>", indexOfEndOfScriptBeginTag, StringComparison.OrdinalIgnoreCase);
                    if (indexOfScriptEndTag == -1)
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.ScriptRegistrationManager_NoCloseTag, activeRegistration.Type.FullName, activeRegistration.Key));
                    }
                    string scriptBlockContents = scriptContent.Substring(indexOfEndOfScriptBeginTag, (indexOfScriptEndTag - indexOfEndOfScriptBeginTag));

                    // Turn the text content into a text attribute
                    attrs.Add("text", scriptBlockContents);

                    lastIndex = indexOfScriptEndTag + 9;
                }

                // Process all the explicit attributes on the script tag
                CaptureCollection attrnames  = match.Groups["attrname"].Captures;
                CaptureCollection attrvalues = match.Groups["attrval"].Captures;
                for (int i = 0; i < attrnames.Count; i++)
                {
                    string attribName  = attrnames[i].ToString();
                    string attribValue = attrvalues[i].ToString();

                    // DevDev Bugs 123213: script elements registered with RegisterStartupScript are normally rendered
                    // into the html of the page. Any html encoded values in the attributes are interpreted by the
                    // browser, so the actual data is not html encoded. We must HtmlDecode any attribute values we find
                    // here to remain consistent during async posts, since the data will be dynamically injected into
                    // the dom, bypassing the browser's natural html decoding.
                    attribValue = HttpUtility.HtmlDecode(attribValue);
                    attrs.Add(attribName, attribValue);
                }

                // Serialize the attributes to JSON and write them out
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                // Dev10# 877767 - Allow configurable UpdatePanel script block length
                // The default is JavaScriptSerializer.DefaultMaxJsonLength
                if (AppSettings.UpdatePanelMaxScriptLength > 0)
                {
                    serializer.MaxJsonLength = AppSettings.UpdatePanelMaxScriptLength;
                }

                string attrText = serializer.Serialize(attrs);
                PageRequestManager.EncodeString(writer, token, "ScriptContentWithTags", attrText);
            }

            CheckScriptTagTweenSpace(activeRegistration, scriptContent, lastIndex, scriptContent.Length - lastIndex);

            if (lastIndex == 0)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.ScriptRegistrationManager_NoTags, activeRegistration.Type.FullName, activeRegistration.Key));
            }
        }
        private void RenderActiveScriptBlocks(List <UpdatePanel> updatePanels,
                                              HtmlTextWriter writer,
                                              string token,
                                              List <RegisteredScript> scriptRegistrations)
        {
            List <RegisteredScript> entriesToRender = new List <RegisteredScript>();
            // no comparer needed because it will contain ScriptKeys which implement Equals
            ListDictionary uniqueEntries = new ListDictionary();

            // For each entry registered in the page, check and see which ones
            // came from controls within UpdatePanels that are going to be updated.
            Control lastControl = null;

            foreach (RegisteredScript entry in scriptRegistrations)
            {
                Control child = entry.Control;

                bool isActive = ((lastControl != null) && (child == lastControl)) ||
                                IsControlRegistrationActive(updatePanels, child, true);

                if (isActive)
                {
                    lastControl = child;
                    ScriptKey scriptKey = new ScriptKey(entry.Type, entry.Key);
                    if (!uniqueEntries.Contains(scriptKey))
                    {
                        entriesToRender.Add(entry);
                        uniqueEntries.Add(scriptKey, entry);
                    }
                }
            }

            foreach (RegisteredScript activeRegistration in entriesToRender)
            {
                if (String.IsNullOrEmpty(activeRegistration.Url))
                {
                    if (activeRegistration.AddScriptTags)
                    {
                        PageRequestManager.EncodeString(writer,
                                                        token,
                                                        "ScriptContentNoTags",
                                                        activeRegistration.Script);
                    }
                    else
                    {
                        WriteScriptWithTags(writer, token, activeRegistration);
                    }
                }
                else
                {
                    PageRequestManager.EncodeString(writer,
                                                    token,
                                                    "ScriptPath",
                                                    activeRegistration.Url);
                }

                string fallbackScriptPath;
                if (_fallbackScripts != null && _fallbackScripts.TryGetValue(new ScriptKey(activeRegistration.Type, activeRegistration.Key), out fallbackScriptPath))
                {
                    // Only encode the fallback path and not the expression. On the client, we would use the success flag on load / readystatechanged to
                    // determine if the script was successfully fetched.
                    PageRequestManager.EncodeString(writer,
                                                    "fallbackScript",
                                                    fallbackScriptPath,
                                                    content: null);
                }
            }
        }