Click here to Skip to main content
14,937,943 members
Articles / Web Development / XHTML
Posted 8 Dec 2007


24 bookmarked

AJAX UpdatePanel With Inline Client Scripts Parser

Rate me:
Please Sign up or sign in to vote.
3.29/5 (5 votes)
9 Oct 2008CPOL
AJAX UpdatePanel with inline client scripts parser.


This control solves the problem when you need inline client scripts within an ASP.NET UpdatePanel to work. Inline scripts, to my knowledge, are not registered to the Script Manager when the "AJAX call" is made. The problem occurs if you have a UserControl, containing an inline script that is dynamically added into an UpdatePanel after an AJAX postback.


If you, like me, already have (had) an existing Web application that's cluttered with inline scripts, and want to take advantage of the features that ASP.NET AJAX provides, you might find yourself with a tedious refactoring job. I managed to solve 99% (1% was invalid markup) of all my inline script problems by using this control.

Hopefully, it can do the same for you!

Using the Code

The control inherits from the original UpdatePanel and uses a Regex to parse for client scripts. You use it just like the original UpdatePanel, and can switch on/off the parsing for inline scripts by setting the property RegisterInlineClientScripts to false.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.IO;

namespace Tuna.Web.Controls
public class TunaUpdatePanel : UpdatePanel
    private static readonly Regex REGEX_CLIENTSCRIPTS = new Regex(
    RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled |
    private bool m_RegisterInlineClientScripts = true;

    /// <span class="code-SummaryComment"><summary></span>
    /// If the updatepanel shall parse and append inline scripts, default true
    /// <span class="code-SummaryComment"></summary></span>
    public bool RegisterInlineClientScripts
            return this.m_RegisterInlineClientScripts;
            this.m_RegisterInlineClientScripts = value;

    protected virtual string AppendInlineClientScripts(string htmlsource)
        if (this.ContentTemplate != null && htmlsource.IndexOf(
            "<script", StringComparison.CurrentCultureIgnoreCase) > -1)
            MatchCollection matches = REGEX_CLIENTSCRIPTS.Matches(htmlsource);
            if (matches.Count > 0)
                for (int i = 0; i < matches.Count; i++)
                    string script = matches[i].Groups["script"].Value;
                    string scriptID = script.GetHashCode().ToString();
                    string scriptSrc = "";

                    CaptureCollection aname = matches[i].Groups["aname"].Captures;
                    CaptureCollection avalue = matches[i].Groups["avalue"].Captures;
                    for (int u = 0; u < aname.Count; u++)
                        if (aname[u].Value.IndexOf("src",
                            StringComparison.CurrentCultureIgnoreCase) == 0)
                            scriptSrc = avalue[u].Value;

                    if (scriptSrc.Length > 0)
                            this.GetType(), scriptID, scriptSrc);
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                            scriptID, script, true);

                    htmlsource = htmlsource.Replace(matches[i].Value, "");

        return htmlsource;

    protected override void RenderChildren(HtmlTextWriter writer)
        ScriptManager sm = ScriptManager.GetCurrent(Page);
        if (this.RegisterInlineClientScripts && sm != null && sm.IsInAsyncPostBack)
            using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))

                string html;
                int outputSize;

                //Get the actual rendering and size
                html = htmlwriter.InnerWriter.ToString();
                outputSize = html.Length;

                //Append inlinescripts and fetch the new markup and size
                html = this.AppendInlineClientScripts(html);
                outputSize -= html.Length;

                //Replace ContentSize if there are any gains
                if (outputSize > 0)
                    html = this.SetOutputContentSize(html, outputSize);


    private string SetOutputContentSize(string html, int difference)
        string[] split = html.Split('|');
        int size = int.Parse(split[0]);
        split[0] = (size - difference).ToString();
        return string.Join("|", split);

Optimization suggestions are welcome!


Thanks to Rogic for solving the issue I hade with replacing the response context to opitmize traffic!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sweden Sweden
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
Member 825063916-Mar-14 23:31
MemberMember 825063916-Mar-14 23:31 
GeneralMy vote of 5 Pin
CyberLight28-Aug-12 1:55
MemberCyberLight28-Aug-12 1:55 
This realisation of updatepanel was helped me to solve my problem with inline script which is included into updatepanel! Greate! Thx a lot!
Questionthanks Pin
parsamiga31-Oct-11 21:12
Memberparsamiga31-Oct-11 21:12 
GeneralSome improvements Pin
Rogic22-Sep-08 1:37
MemberRogic22-Sep-08 1:37 
GeneralRe: Some improvements Pin
Baxterboom7-Oct-08 3:00
MemberBaxterboom7-Oct-08 3:00 
GeneralRe: Some improvements [modified] Pin
Baxterboom8-Oct-08 5:34
MemberBaxterboom8-Oct-08 5:34 
AnswerRe: Some improvements Pin
Rogic21-Oct-08 0:49
MemberRogic21-Oct-08 0:49 
GeneralSo close..... Pin
Owen3713-Dec-07 7:26
MemberOwen3713-Dec-07 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.