Search code examples
javascriptasp.netgoogle-analyticspostbacklinkbutton

Postback fails on ASP.NET page when using Google Analytics


I am using ASP.NET to create a small web app. One of my pages includes some LinkButton controls, which used to work fine until I added a reference to Google Analytics code to it. Now, when I click on a link button, I get the error message:

Microsoft JScript runtime error: The value of the property '__doPostBack' is null or undefined, not a Function or object

The other links and controls on the page work fine. If I remove the reference to the Google Analytics script from the page, everything works fine as well. It seems that the problem arises from the interaction between the Google Analytics script and the LinkButton controls trying to postback the page.

UPDATE. I have further observed the following. When no Google Analytics script reference is present, the HTML that ASP.NET generates looks fine:

HTML structure with no Google Analytics code

However, as soon as I add Google Analytics code, the HTML gets screwed:

HTML structure with Google Analytics code

Check out that form tag! I imagine now that the postback error arises from the fact that the linkbutton controls get placed outside the ASP.NET form. But why? END UPDATE.

Any ideas on how to solve this? Thanks.

FURTHER UPDATE. After much experimenting, I have been able to solve this by myself. I have added an answer below showing my conclusions. Thanks to all who have posted answers here. END UPDATE.


Solution

  • After lots of tweaking, tech support and experimenting, I have fixed the problem. My code looked like this:

    <head runat="server">
        <title><asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder></title>
        <link href="_private/Normal.css" rel="stylesheet" type="text/css" />
        <script src="_private/GoogleAnalytics.js" type="text/javascript" />
    </head>
    

    This caused the problem that I describe in my OP. Everything is fixed when I switch to the following:

    <head runat="server">
        <title><asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder></title>
        <link href="_private/Normal.css" rel="stylesheet" type="text/css" />
        <script src="_private/GoogleAnalytics.js" type="text/javascript"></script>
    </head>
    

    Notice that the script is now closed by a full closing tag rather than a self-closing tag. I assumed that script could be self-closing with no side effects, but apparently things behave differently when you close it differently. This sheds some light on why.