This post is about registering startup script in the ASP.NET WebForms inside
UpdatePanel. The method we used
ScriptManager.RegisterStartupScript takes 5 arguments, four of them are quite obvious. From the documentation:
The last one entirely changes the behavior and leads to some unexpected behavior and eventually exceptions.
When the parameter
true, everything works as expected, at least to me. The script is encoded and registered inside
<script> tag created by the ASP.NET.
But when we register startup script with
false value for the parameter
addScriptTags. Without changing anything in the script (except adding
<script> tag) the page stops working. The end user doesn't see anything, except the missing update of the UpdatePanel.
Also, when logging unhandled exceptions in the web application (
HttpApplication.Error), you got following
at System.Web.UI.ScriptRegistrationManager.WriteScriptWithTags(HtmlTextWriter writer, String token, RegisteredScript activeRegistration)
at System.Web.UI.ScriptRegistrationManager.RenderActiveScriptBlocks(List`1 updatePanels, HtmlTextWriter writer, String token, List`1 scriptRegistrations)
at System.Web.UI.ScriptRegistrationManager.RenderActiveScripts(List`1 updatePanels, HtmlTextWriter writer)
So, the ASP.NET starts using
addScriptTags is changed to
false. I'm not sure why this happens, maybe some kind of encoding? But why they use
The source is taken from the reference source of the ScriptRegistrationManager and shortened for clarity.
private static void WriteScriptWithTags(HtmlTextWriter writer, string token, RegisteredScript activeRegistration)
string scriptContent = activeRegistration.Script;
OrderedDictionary attrs = new OrderedDictionary();
string scriptBlockContents = scriptContent.Substring(indexOfEndOfScriptBeginTag, (indexOfScriptEndTag - indexOfEndOfScriptBeginTag));
if (AppSettings.UpdatePanelMaxScriptLength > 0)
serializer.MaxJsonLength = AppSettings.UpdatePanelMaxScriptLength;
string attrText = serializer.Serialize(attrs);
PageRequestManager.EncodeString(writer, token, "ScriptContentWithTags", attrText);
After registering a script (including script tags) the ASP.NET parses out all these tags. For each of the found script tag, it creates a dictionary with the script content (
your script), eventually adds attributes from the tag and uses
maxJsonLength, which can be overriden by the appSettings with key
This appSetting is parsed as
Int32, which can be found at reference source of the AppSettings. There are also other 'hidden' ASP.NET appSettings.
The serialized dictionary is then placed in the ajax response, deserialized at the client and executed by the browser.
There is no limit for the length of the script registered with the
true. When this is changed, the script is serialized using
It is always better to register scripts without tags and let the infrastructure to generate them for you. I even don't known any use-case where I would want to generate them by myself and if you do, please leave a comment. Nevertheless I have found this usage on many places in the customers code base.