Thursday, August 30, 2012

ASP.Net and the dreaded Microsoft JScript runtime error: Object doesn't support this property or method

A little background on JScript.  Microsoft, in typical Microsoft fashion, grew weary of dealing with Sun when it came to JavaScript.  So, it decided to develop its own language.  The problem is that it is the exact same language as JavaScript.  So how does Microsoft get around the trademark issue? 
In 1975, Bill Gates founded Microsoft.  However it was not until 1980 when Microsoft had its first Operating System (OS).  Ironically, it was a Unix based system called Xenix.  In November 1980, IBM awarded Microsoft a contract (I actually worked for the man who interviewed Bill Gates in the newly acquired offices of Microsoft that until the day before the meeting had no power or phone lines when I worked at IBM) to provide an OS for their new personal computer division.  Microsoft in turn bought 86-DOS from Seattle Computer Products.  Microsoft then took the product, renamed it MS-DOS and to fulfill their contract with IBM sold it to IBM as PC-DOS.  PC-DOS was essentially the same product as MS-DOS and 86-DOS, well except for the name. 
As with most business contracts, the relationship soon soured.  Microsoft did not approve of IBM’s marketing techniques and the relationship faulted amicably.  The point here is that Microsoft still had its own product named MS-DOS and IBM still owned the product that Microsoft sold to it called PC-DOS.  They were the same product, with few exceptions that grew over time.
In order for Microsoft’s Internet Explorer to support JavaScript, Microsoft obviously had to know the ins and outs of the language.  However, this came with a price in which Microsoft was paying Sun Systems for JavaScript rights to include in its browser.  Eventually, Microsoft decided that it no longer needed to pay for the rights of JavaScript and developed JScript.  Initially, it was an exact copy of JavaScript, however over time it, much like PC-DOS and MS-DOS, has developed differences between the two.  Thus, Microsoft had its own JavaScript engine called JScript.
Obviously, there are other items in the timeline that I have skipped over for instance JavaScript was initially developed by Netscape for its 2nd version of its popular browser called Netscape 2.  Originally conceived, the language was named LiveScript.  It was developed as a scripting language that could be interpreted by the browser at runtime and based loosely on C.

Every ASP.Net page uses the JScript engine in the background to keep up with things like ViewState and other small items that need tidying up.  The problem is that ASP.Net utilizes a single form model.  This means that every single page submits to itself.  Why is this important?  Well let’s take a look.

In the stack, there is a method for the page called Render.  This is the method that starts processing all the controls on the page.  It looks for the form tag to start processing all the controls.  Once it hits the closed form tag, it knows that it has processed all the controls on the form and sets a flag.  As the page processing continues and if it hits another form tag, it then hits an exception (because through the flag, it knows it has already processed the form).
This hasn’t created a problem for us yet, as we have not posted the form back to itself using HTTP POST.  As of yet, we as developers are unaware of any problem. 

The error message received when you have more than one form on a page.

Using the single form model, it can be easily deduced that you can name the form form1 and expect it to be named this at all times (on every web page).  Finally, this being the case, you can hard code the name for reference as we can see here in the Microsoft JScript engine:

This makes the form post unto itself.  How do we accomplish that?  By making the form always post to form1.  So if you have another form on the page, especially if it is not named form1, you can see how this can cause problems.

Typically the JScript error appears when you have an additional form on the web form.  It can also happen when you have changed the name of the form.  Another thing to check for is to make sure your opening tags are all closed.  This can render a perfectly formatted form tag inoperable.  The final check would be the tags on your master page (if using one) or outside of the form tag (when not using master pages).  If your HTML or BODY tags are muffed, (to use a Georgia term) It just ain’t gonna werk.

Happy .Netting…

No comments:

Post a Comment