ASP.Net 4.5.1 LinkButton - The value of the property '__doPostBack' is null or undefined, not a Function object

Posted by Blake on 9/3/2015
)

I was attempting to use a LinkButton in an ASP.Net WebForms application and I was receiving the following error which occurred simply on a PostBack with no other code.

Error

    Line: 1
    Error: The value of the property '__doPostBack' is null or undefined, not a Function object

There are many blog posts that tell you that one cause of this is ASP.Net not correctly detecting your browser. It uses browser definition files and when new browsers come out it doesn't quiet know what to do with it without a manual intervention or an update from Microsoft. Here are the links where Scott Hansleman explains these scenarios:

In my scenario this wasn't the case. The interesting thing was, when I manually went into the browsers script debugging page I was able to paste in "__doPostBack('linkButtonTest','')" and have it correctly execute. When I inspected the HTML/JavaScript to see what was actually running I saw that it was the same line, only encoded... and when I ran that line through the script debugger it failed with the same error. That let me to a blog bost from Microsoft of breaking changes in the .Net Framework 4.0

HtmlEncode and UrlEncode Now Encode Single Quotation Marks

In ASP.NET 4, the HtmlEncode and UrlEncode methods of the HttpUtility and HttpServerUtility classes have been updated to encode the single quotation mark character (') as follows:

 - The HtmlEncode method encodes instances of the single quotation mark as ' . 
 - The UrlEncode method encodes instances of the single quotation mark as %27. 

Workaround

How do you work around this? You could potentially have controls render in 3.5 mode. You could potentially use the httpRuntime to override the default encoding. Or, you could try a workaround hack (and that is what I will share). This will fix a single control and not affect the entire site. I'll use the LinkButton control, put the __doPostBack javascript in the OnClientClick and include an href to # so the page stays put (this will not keep your scroll position unforunately... although in my scenario I was sending a file through the http response so I didn't care, it also wasn't a long page for me).

<asp:LinkButton ID="linkViewCoverLetter" runat="server" Text="View Cover Letter" href="#" OnClientClick="__doPostBack('linkButtonTest','')"></asp:LinkButton>

Update: You may additionally need to add a CausesValidation="false" property to the LinkButton. I ran into an issue in a server control on an HTML5 site that wouldn't work without that additionally being set.

Is this a great workaround? Probably not and it may not be ideal for your scenario. Will it work. Yup. I've tested it in Chrome 45, IE 10/11 and Firefox 40.0.3.