Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pageLoad() not firing in a dynamically loaded page using .NET 4.0 (Works fine on 3.5)

Tags:

jquery

asp.net

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

like image 568
Paul Avatar asked Oct 21 '25 12:10

Paul


1 Answers

The special pageLoad function is called when the Microsoft Ajax Library has finished initializing the Sys.Application object. Unfortunately, this initialization code was drastically rewritten in ASP.NET 4.0 (details below), and now, in standards-compliant browsers, Sys.Application doesn't get initialized at all when a page is loaded by the jQuery load function. Fortunately, there's a simple workaround: initialize Sys.Application yourself:

$('#myRightdiv').load('Test.aspx', function () { Sys.Application.initialize(); });

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 pageLoad function 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 to Sys.Application.initialize() right before the </form> tag. When jQuery loads Test.aspx, it executes each <script> in sequence, so your pageLoad is defined before Sys.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], the Sys.Application constructor adds a DOMContentLoaded event listener that calls Sys.Application.initialize(). But DOMContentLoaded was 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-raise DOMContentLoaded.

  • In browsers that support document.attachEvent but not document.addEventListener [such as IE9 (Quirks mode)], the Sys.Application constructor calls Sys.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 your pageLoad function before MicrosoftAjax.js is included; a good place is the <head>.

like image 138
Michael Liu Avatar answered Oct 23 '25 05:10

Michael Liu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!