I have a simple aspx page with a link to load another aspx page on right div (using JQuery load), like that:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html>
<head>
<script type="text/javascript" src="Scripts/jquery-1.6.2.js"></script>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function loadPage() {
$('#myRightdiv').load('Test.aspx', function () {});
}
</script>
<asp:Button ID="Button1" runat="server" Text="Load ..." OnClientClick="loadPage(); return false;" />
<div id="myRightdiv"></div>
</form>
In my Test.aspx I have a pageLoad like that:
<%@ Page Title="Test" Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="About" %>
<html>
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<script type="text/javascript">
function pageLoad() {
alert("pageloaded");
}
</script>
</form>
</body>
</html>
That works fine on ASP .NET 3.5 …. But using .NET 4.0 the pageLoad is not fired!
Any idea or workaround?
Thanks
The special
pageLoadfunction is called when the Microsoft Ajax Library has finished initializing theSys.Applicationobject. Unfortunately, this initialization code was drastically rewritten in ASP.NET 4.0 (details below), and now, in standards-compliant browsers,Sys.Applicationdoesn’t get initialized at all when a page is loaded by the jQueryloadfunction. Fortunately, there’s a simple workaround: initializeSys.Applicationyourself:I tested this fix in IE9 (Standards mode), Firefox, and Chrome.
To get your example to work in IE9 (Quirks mode), you also need to move your
pageLoadfunction up into the<head>.What Changed Between ASP.NET 3.5 and ASP.NET 4.0
In ASP.NET 3.5, the
<asp:ScriptManager>control inserts a call toSys.Application.initialize()right before the</form>tag. When jQuery loads Test.aspx, it executes each<script>in sequence, so yourpageLoadis defined beforeSys.Application.initialize()calls it, and everything works.In ASP.NET 4.0, the
<asp:ScriptManager>control no longer inserts that bit of JavaScript. Instead:In browsers that support
document.addEventListener[such as IE9 (Standards mode), Firefox, and Chrome], theSys.Applicationconstructor adds aDOMContentLoadedevent listener that callsSys.Application.initialize(). ButDOMContentLoadedwas already raised when the browser finished loading the current page (Default.aspx). Loading Test.aspx via an AJAX request won’t cause the browser to re-raiseDOMContentLoaded.In browsers that support
document.attachEventbut notdocument.addEventListener[such as IE9 (Quirks mode)], theSys.Applicationconstructor callsSys.Application.initialize()if the browser has finished loading the current page (which it has, since the current page is Default.aspx). In this case, you need to define yourpageLoadfunction before MicrosoftAjax.js is included; a good place is the<head>.