Wednesday, March 21, 2012

LoadScriptsBeforeUI and Web.config debug=true

I've searched around for an answer for this particular situation, but I haven't found a good answer. I'm hoping someone can explain what is going on.

The situation is that I have a very simple aspx page, containing an UpdatePanel. The UpdatePanel contains an Asynchronous Trigger, tied to a Timer, which is also within the UpdatePanel. The Script manager has various attributes, but the key things are that it has the attribute LoadScriptsBeforeUI = "False", and it also references a custom javascript file. That javascript file simply ties some alerts to the request events. I have included the source below.

The problem is that when the web.config has the compilation debug=true flag set, this page throws an error. Basically, when the timer executes, between the BeginRequest and EndRequest events, the following error appears:

Sys.InvalidOperationException: Type Sys.UI._Timer has already been registered

Basically, on the AJAX response, it's attempting to reregister the Timer type with the AJAX framework, via the RegisterClass() method in the AJAX framework. I've traced this back to the fact that when the debug is set to true in the web.config, the AJAX framework uses the debug version of the scripts, which throws this error. The production version of the AJAX scripts does not look for this error, nor does it care whether something has been registered twice.

Some other relevant facts:

a. Whether or not debug is set, if LoadScriptsBeforeUI is removed (or set to true), the re-registration error doesn't occur, but the custom javascript file is unable to complete, because the Sys object apparently doesn't exist when that custom javascript file starts running.

b. Whether or not debug is set, if LoadScriptsBeforeUI is removed (or set to true) AND the ScriptReference to the custom javascript file is removed, this re-registration error doesn't occur. The page works fine. Odd, because the error doesn't seem to reference the custom javascript file, it references the Timer.

c. Whether or not debug is set, if LoadScriptsBeforeUI is kept at false, AND the ScriptReference to the custom javascript file is removed, the re-registration error occurs. So, at a minimum, the problem is between the LoadScriptsBeforeUI=false and the behavior of the response.

So, boiled down, this is a very simple page. When the debug= true, AND the LoadScriptsBeforeUI=false it throws that strange error. What I need to know is:

1. Aside from the debug setting, why is the response trying to re-register the Timer object with the page? And why does it depend on the

2. On a side note, is there a way to set LoadScriptsBeforeUI =true (or leave it out), AND have the custom javascript file able to reference the Sys object?

3. Generally... what's up? I've tested this on a few machines, and it's all the same.

If I need to clarify some things, let me know. Below is the source:

===============================

UnitTest1.aspx

===============================

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeBehind="UnitTest1.aspx.cs" Inherits="Portal.UnitTest1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="PortalSM" runat="server" EnablePageMethods="True" EnableScriptGlobalization="True"
EnableScriptLocalization="True" LoadScriptsBeforeUI="False" EnablePartialRendering="true">
<Scripts>
<asp:ScriptReference Path="UnitTestJavascript.js" />
</Scripts>
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick" />
</Triggers>

<ContentTemplate>

<asp:Timer ID="timer1" runat="server" Interval="1000"></asp:Timer>

<asp:Label runat="server" ID="time2"></asp:Label>

</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

===============================

UnitTest1.aspx.cs

===============================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;

namespace Portal
{
public partial class UnitTest1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(100);
string theTime = DateTime.Now.ToLongTimeString();
for (int i = 0; i < 3; i++)
{
theTime += "<br />" + theTime;
}
time2.Text = theTime;
}
}
}

===============================

UnitTestJavascript.js

===============================


_fired1 = false;
_fired2 = false;
_fired3 = false;


Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestHandler);


function InitializeRequestHandler(sender, args)
{
if (!_fired1)
alert("Initializerequest");

_fired1 = true;
}

function BeginRequestHandler(sender, args)
{
if (!_fired2)
alert("Beginrequest");

_fired2 = true;
}

function EndRequestHandler(sender, args)
{
if (!_fired3)
alert("Endrequest");

_fired3 = true;
}


if (typeof('Sys') !== 'undefined')
Sys.Application.notifyScriptLoaded();

I may have explained it too complicated, but it should be an easy question. Anyone know?


I'm looking into this... I'll let you know what I find shortly.


It's a confirmed bug that will be fixed in the next release. Basically, it has to do with how the PageRequestManager detects which scripts have already been loaded on the page. With LoadScriptsBeforeUI=true, the URLs match, with LoadScriptsBeforeUI=false, they differ by an encoded ampersand (& != &), so the script attempts to load a 2nd time.

Do you have to use LoadScriptsBeforeUI=false, or is true acceptable as a workaround in the meantime? If you need a workaround that enables LoadScriptsBeforeUI=false let me know.



Thanks, that's great information. When you say next release, is the next release going to be an AJAX-only release version, or the full .NET framework with AJAX bundled in (3.1, 3.5, or whatever)?

The only reason I have to use LoadScriptsBeforeUI = false, is because the custom javascript file (UnitTestJavascript.js) is attempting to attach events to the Sys object (as shown above). If LoadScriptsBeforeUI = true, then the Sys object doesn't exist when the custom javascript is being run, and those event attachments throw an error about Sys not being an object.

So, I guess the next question would be:

How can I ensure that the Sys object is available for use in my custom javascript file (UnitTestJavascript.js), without delaying it to the end by using LoadScriptsBeforeUI? If there's a way to make the custom script wait somehow, that would be good.

This behavior is somewhat unexpected, because I more or less assumed that the custom javascript file would not be loaded until the rest of the true AJAX code had been executed, and the sys objects existed.

Again, thanks!


Just put your JavaScript after the ScriptManager tag.


Hi,

are you referencing the custom file through the ScriptManager control?


If you are using any Custom Javascript file then use ScriptManager to load the JS file like the following:

<asp:ScriptManager ID="TheScriptManager" runat="server">
<Services>
<asp:ServiceReference Path="~/DataService.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="~/YOUR JS FILE" />
</Scripts>
</asp:ScriptManager>

Or If you have javascript embedded in Aspx page then add it after the ScriptManager, like following:

<asp:ScriptManager ID="TheScriptManager" runat="server"></asp:ScriptManager>
<script type="text/javascript">
//YOUR JS CODES
</script>


I don't think the problem is that Sys is not defined, its that PageRequestManager.getInstance is returning null. You are attempting to use the request manager before its been initialized. The ajax framework is always loaded first, so Sys should definitely be there. Just define an application init event and add your request manager events from there --

Sys.Application.add_init(function() { /* add request manager events here */ } );


Hi you can use external javascript file with scriptmanager like this Smile

<asp:ScriptManager ID="TheScriptManager" runat="server">
<Scripts>
<asp:ScriptReference Path="~/YOUR JS FILE" />
</Scripts>
</asp:ScriptManager>


Ah, great idea. Again, thanks for the information. Knowing that was a known bug was very helpful.

Thanks!

No comments:

Post a Comment