Showing posts with label breaks. Show all posts
Showing posts with label breaks. Show all posts

Wednesday, March 28, 2012

AJAX breaks URL rewriting : Microsoft JScript runtime error: Sys is undefined

Hey all,

My development machine is Windows XP. I'm using Intelligencia's URLRewriter.NET httpmodule to handle URL rewriting. I'm also using ScottGu's Form Control Adapter trick to catch postbacks.

Here's the problem - Sometimes, AJAX will fail. The controls inside the UpdatePanels will work. However, they will cause the whole page to refresh instead of just the area inside the UpdatePanels. This bug happens randomly. After days of working on this problem, I have not found a way to reproduce the bug. This has been very, very frustrating.

When I attach the Visual Studio debugger to IE, the page will break on this line :
Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));

This is the error that I get :
Microsoft JScript runtime error: 'Sys' is undefined

When I try to debug the page in Firefox using the FireBug plugin, I get the five following errors :

I'll get three errors for the following line :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
One of those errors comes from WebResource.axd, and two of them come from ScriptResource.axd

I'll get a "Sys is not defined" error from my index.aspx, although FireBug apparently doesn't know which line is throwing the error.

Finally, I'll get a "Sys is not defined" error from this line in my index.aspx:
Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));

I can solve the problem temporarily by disabling the UrlRewriter. There's a line in web.config that specifies when the URLRewriter should rewrite a URL. After I take this line out of the web.config, I'll reload a page on the site, and AJAX will be working fine again. I'll then put the line back in web.config, thus enabling the URLRewriter. AJAX will continue to work fine. In other words, by taking out the URLRewriter and putting it back in again, the problem will be solved until the next time it randomly happens again. Weird, huh?

If you have any insight at all into the nature of this problem, please let me know. Likewise, if you can tell me how to debug this monster, please let me know, as I am at an utter loss at this point.

I believe UrlRewriter allows you to add exclusions based on header data. If so, exclude any requests with the request header "X-MicrosoftAjax: Delta=true". That will prevent it from interfering with any partial postbacks.

Thanks for the advice! This bug has driven me *insane!* I swear, I must have spent at least 2 1/2 days on this thing.

Fortunately, I think I made a breakthrough today.

URLRewriter.NET requires that you specify which URLs to intercept. Naively, I had given it the following configuration :
<rewrite url="(.*)" to="${UrlTransformer($1)}" /
Basically, I had told it to intercept all incoming URLs. I think this was a mistake.

Somebody on a forum suggested that, if you get the "Sys is undefined" error, you should try to take the URL for the ScriptResource.axd file and paste it into your address bar and see what happens. When I did that, instead of being offered the opportunity to download ScriptResource.axd, I noticed that URLRewriter had picked up on the URL and was trying to do a rewrite! This could mean that IIS never properly received the request for ScriptResource.axd, and thus the client never recieved it. This would explain the JScript error and subsequent AJAX failure.

I went ahead and entered the following new configuration for URLRewriter :
<rewrite url="^(.*\.aspx)$" to="${UrlTransformer($1)}" />

So now, it's only picking up URLs that end in .aspx. So far, everything is working exactly as it should. However, I am hesitant to close the case on this for two reasons :

1) This bug has been incredibly hairy, and there have been a few other occasions where I was *sure* I had solved the problem. Thus I am hesitant to call it prematurely.
2) The incredibly random nature of the bug. This is really what made this bug so difficult; the fact that there was no reliable way to reproduce it. Even if I am correct about the cause of the bug, why in the world would it occur so randomly? I know that the browser will cache the ScriptResource.axd code. Could the bug's occurance coincide with the browsers' cache being flushed? Was this problem really more likely to occur when the network was slow, or was I just imagining things? Why would removing the line from the web.config and then replacing it again make a difference?

I may never know the answers to these questions.

In any case, thank you for your help. Your suggestion will be the first thing that I try if my current solution doesn't work.

AJAX Breaks the asp:Treeview

It is also a generally unstable browsing and development experience, but there's enough posts on that already.

I have the .Net AJAX RC installed. Create a simple AXAJ enabled web site, add a Label to display the selected node values, and a treeview using an xmldatasource. The XML has to be somewhat deeply nested to get the bug, about 6 levels deep. Wrap it all in an update panel and watch it explode when you try to navigate the tree.

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
this.TreeNodeValue.Text = TreeView1.SelectedNode.Value + "::" + TreeView1.SelectedNode.Text;
}

<body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="updTreeView" runat="server"> <ContentTemplate> <asp:Label ID="TreeNodeValue" runat="server" /> <div> <asp:XmlDataSource ID="dsTreeview" runat="Server" XPath="entity" DataFile="~/App_Data/ReportsTree.xml" /> <asp:TreeView ID="TreeView1" runat="Server" DataSourceID="dsTreeview" ExpandDepth="2" NodeIndent="8" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"> <RootNodeStyle CssClass="RootNode" /> <ParentNodeStyle CssClass="ParentNode" /> <LeafNodeStyle CssClass="LeafNode" /> <HoverNodeStyle CssClass="HoverNode" /> <SelectedNodeStyle CssClass="SelectedNode" /> <NodeStyle CssClass="Node" /> <DataBindings> <asp:TreeNodeBinding DataMember="entity" TextField="name" ValueField="SubjectID" PopulateOnDemand="true" ToolTipField="name" ImageToolTipField="ResourceType" /> </DataBindings> </asp:TreeView> </div> </ContentTemplate> </asp:UpdatePanel> </form></body>
 
 
<entity SubjectID="1" ResourceType="Company" ImageURL="images/ResourceIcons/Company.gif" name="Company"><entity SubjectID="2" ResourceType="Application" ImageURL="images/ResourceIcons/Application.gif" name="Impact Analysis"><entity SubjectID="3" ResourceType="Scope" ImageURL="images/ResourceIcons/assembly.gif" name="Horizon"><entity SubjectID="4" ResourceType="Namespace" ImageURL="images/ResourceIcons/namespace.gif" name="Bip.Horizon.Holding"><entity SubjectID="5" ResourceType="ClassList" ImageURL="images/ResourceIcons/Folder.gif" name="Classes"><entity SubjectID="6" ResourceType="Class" ImageURL="images/ResourceIcons/Class.gif" name="Bip.Horizon.Holding.Holding" Modifier="Public" ClassType="Class"><entity SubjectID="7" ResourceType="MethodList" ImageURL="images/ResourceIcons/Folder.gif" name="Methods"><entity SubjectID="8" ResourceType="Method" ImageURL="images/ResourceIcons/Method.gif" name=" 'BIP.Horizon.Holding.Holding.GetProfileMarketCapAllocations" Modifier="Public"><entity SubjectID="9" ResourceType="Return Type" ImageURL="images/ResourceIcons/Class.gif" name="Return Type" DataType="System.Data.DataTable"></entity><entity SubjectID="10" ResourceType="Argument List" ImageURL="images/ResourceIcons/Folder.gif" name="Arguments"><entity SubjectID="11" ResourceType="Argument" ImageURL="images/ResourceIcons/Column.gif" name="dsRebalanceAdjustedHoldingsByAccount" DataType="BIP.Horizon.Holding.RebalanceAdjustedHoldingsByAccountDS" type="BIP.Horizon.Holding.RebalanceAdjustedHoldingsByAccountDS" Modifier="ByRef"></entity><entity Modifier="ByRef" SubjectID="12" ResourceType="Argument" ImageURL="images/ResourceIcons/Column.gif" name="dsProductMarketCapRange" DataType="BIP.Core.Product.ProductMarketCapRangeDS" type="BIP.Horizon.Holding.RebalanceAdjustedHoldingsByAccountDS"></entity><entity Modifier="ByRef" SubjectID="13" ResourceType="Argument" ImageURL="images/ResourceIcons/Column.gif" name="intMarketCapRangeID" DataType="int" type="int"></entity></entity><entity SubjectID="14" ResourceType="Call List" ImageURL="images/ResourceIcons/Folder.gif" name="Callers"><entity SubjectID="15" ResourceType="Method" ImageURL="images/ResourceIcons/Method.gif" name="BIP.Horizon.Web.UI.Account.aspx.cs"></entity></entity><entity SubjectID="16" ResourceType="Callee List" ImageURL="images/ResourceIcons/Folder.gif" name="Callees"><entity SubjectID="17" ResourceType="Method" ImageURL="images/ResourceIcons/Method.gif" name="BIP.Horizon.Product.GetMarketCapRanges"></entity></entity><entity SubjectID="18" ResourceType="Data Object List" ImageURL="images/ResourceIcons/Folder.gif" name="Data Objects"><entity SubjectID="19" ResourceType="DataObject" ImageURL="images/ResourceIcons/Database.gif" name="spbip.Horizon.Product.GetMarketCapRanges"></entity></entity></entity></entity></entity></entity></entity></entity></entity></entity>

 
 

The treeview control is not supported in the updatepanel by the docs. I hope this will be change in the future or we'll get a new real ajax treeview. I am using the treeview in updatepanel since the first ctps. I have some problems with it (for example with EnableClientScript="true"), but with some hacks it works for me.

The main problem for me that the treeview+updatpanel could be very slow, because the treeview needs enableviewstate="true" to works properly.Sad

First I world try to turn off any extra on the treeview especially this:

PopulateOnDemand="true" ->PopulateOnDemand="false"
I didn't try your code by I suppose that populating the nodes on the client side then expanding/collapsing/selecting a node could be easilystay out of the sync with the viewstate/server side.That was my problem with EnableClientScript="true".


AJAX breaks AdRotator control rendering

Hi,

Does anyone know how to resolve the issue I'm experiencing with AJAX breaking the AdRotator control?

My AdRotators reside in a master page and display fine until a AJAX UpdatePanel is introduced. Once an UpdatePanel is added the AdRotators start to render their HTML output at the very beginning of the page's HTML (before the

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
in my case).

Image without AJAX problem:http://x.yougodo.com/screenshot-good.jpg

Image with AJAX problem:http://x.yougodo.com/screenshot-bad.jpg

Any help would be greatly appreciated.

Cheers

I can't seem to find any other users with your issue. You may want to give this approach a try:http://www.seesharprun.net/2007/08/09/AnAdrotatorAlternative.aspx. Where is your AdRotator located on your MasterPage? I'm assuming you are adding your UpdatePanel inside the Master's ContentTemplate?

Perhaps you can post a small snippet of code that will replicate the issue?

-Damien


Hi Damien,

Thanks for your suggestion, much appreciated - I will take a look.

Was puzzled myself why no-one else seemed to have encountered the same problem?!?

The UpdatePanel is actually located in the *.aspx page rather than the *.master - I'll try and create a small example to re-create later as everything is too intertwined in my current project to post something useful.

P.S. The Google Ads in the screenshots are not in AdRotators, only the image ads you see.


How bizarre... I've not been able to recreate problem creating a simple AJAX / master page scenario?!?

Maybe it's down to some of my non-standard controls - all pages are inherited from my own Page class that adds a bunch of extra properties and methods useful for my project. The control triggering the AJAX callback is also my own roll-your-own page number selecting control.

Quite puzzled as both these own-code-rolled controls successfully use AJAX functionality on other pages. It's jut when the AdRotator is introduced that bizarre results occur. Quite puzzled!


That's interesting that you can't reproduce it. I think your assumption may be correct with your non-standard controls. Do you perhaps have an un-closed element or something? This could be causing some issues. You also may just want to make sure your AJAX environment is setup correctly, you can refer to these common problems/fixes:http://blogs.visoftinc.com/archive/2007/09/23/asp.net-ajax--updatepanel-not-working--common-problems.aspx.

Hope you find your problem!

-Damien


Hi Damien,

Cheers once again for your help - much appreciated. Doesn't seem to be any of the issues described in the link you gave though.

Very bizarre, the output would suggest that somehow the AdRotator is getting to render it's output before the *.master or *.aspx files do?!? No html at all is output to the locations where the AdRotator is placed, only at the top of the page before all other html. Very puzzling.

I've abandoned trying to find a solution for now, coding my own temporary random ad selector for now. The AdRotators were only ever going to be a temporary measure until a fully functioning ad management system is plugged in.

Gavin.