Wednesday, January 30, 2008

‘WebForm_PostBackOptions’ is undefined, Validators and WebResource.axd

If you have ever come across the JavaScript error message ‘WebForm_PostBackOptions’ is undefined in your travels through the world of ASP.NET, you will know just what a pain it is. Hopefully, the following will go some way to helping resolve it:

Firstly, when does it happen?

You will typically see this error message when you display an ASPX page that contains a Validator (for example a RequiredFieldValidator), some controls and a control that causes a postback to the server. The JavaScript error message is displayed when you invoke the postback and the client controls are validated; it may just be displayed and then continue to allow the postback; it may be displayed and actually prevent the postback altogether.

Secondly, why does it happen?

Validators can use client side JavaScript to validate the controls without posting back to the server – this give a far better user experience. Obviously, when an error occurs, quite a large amount of JavaScript is required to locate the bound control, perform the validation, display an error message in the correct location on the screen and prevent the postback. It would be good if this JavaScript were cached on the client rather than being added to the HTML directly.

One way of caching the JavaScript is to place it into a separate .js file. This file can then be referenced directly in the HTML, downloaded and cached on the client. Using this approach, the .js file would need to be available on the web server for download, which isn’t the end of the world, but does involve a management / deployment overhead.

ASP.NET 2.0 introduced the concept of the WebResource.axd handler (i don’t intend to go into detail as to what this handler is/does, but more information can be found here). This handler can be used to ‘look inside’ assemblies for resources such as images, strings or files such as JavaScript or CSS files. This provides a neat solution to deployment and reuse as suddenly our Validators become a single assembly that contains all of the client and server side logic.

If you look at the HTML that is generated when a Validator is on an ASPX page, and EnableClientScript = True, you will see that a reference to a script file is added to the output, an example of which is shown below. This reference points back to the client side JavaScript required to perform validation. The reason we see the ‘WebForm_PostBackOptions’ is undefined error is because there has been a problem downloading the JavaScript file from the server – of course this isn’t highlighted in the browser and so isn’t immediately obvious.

<script type="text/javascript" src="/WebSite1/WebResource.axd?d=VqPlh_vtOSm3C5JXuOJFWDaG0dm1LwfB7--yzOqTmJw1&t=633210572006621533"></script>

How can we find out what the exact problem is?

This is actually very difficult. There is no tracing on the server for the WebResource.axd handler so we do not know if there is a problem obtaining the requested resource. Similarly, a lot of the initial errors, such as the script file not being downloaded, are swallowed by the browser. However, it is possible to place the WebResource.axd link directly into the browser and see what is returned. If everything is OK, the returned content will be a JavaScript file; otherwise the return value should give us an idea as to the cause of the problem.

Typically, the error is due to the Web Server being configured to perform HTML parsing on returned content and the contents of the AXD response are being altered. For example, there may be a handler that adds a company header and footer to the response, which in turn invalidates the JavaScript. Alternatively, there may be some form of compression that takes place on the output, which in turn invalidates the content.

Hopefully, armed with this information, you should be able to track down the problem. If all else fails, you can either switch EnableClientScript = False, to prevent the addition of the WebResource.axd script link to the HTML – you will need to perform server side validation and suffer a roundtrip to the server though. Alternatively, you will need to write your own validation routine.

Thursday, May 17, 2007

AJAX Progress

Looks at creating a general use, multipurpose progress indicator that automatically informs visitors that an AJAX request in progress.

See this content on www.pocketnerd.net -->

Thursday, October 05, 2006

Xml Color

This is a wrapper class for System.Drawing.Color, that enables a color to be Serialized and Deserialized

See this content on www.pocketnerd.net -->

Timed Dictionary

The TimedDictionary provides functionality that is similar to a normal Dictionary except the items within it can expire and be cleaned up

See this content on www.pocketnerd.net -->

Friday, March 17, 2006

Generic Name Value Pair

This is a simple class that is used to associate a Name with a corresponding Generic Value and to add Items to ComboBoxes, ListBoxes etc

See this content on www.pocketnerd.net -->

Wednesday, March 08, 2006

Sharing Application Settings

This .NET 2.0 bit of code shows how application settings in one assembly, typically an EXE, can be shared with referenced assemblies, typically DLLs.

See this content on www.pocketnerd.net -->

Monday, February 27, 2006

Feedback Provider

Sample code that shows how to create a Feedback Provider, similar to an ErrorProvider, that can display Info, Warn, Error, Dirty and Mandatory icons

See this content on www.pocketnerd.net -->

Saturday, February 18, 2006

Downloading Webpage Data into an Application

This article looks at how we can enable our website visitors to download data from our webpages to view or manipulate it in their favorite applications. In particular, we are going to look at how we can export individual tables and complete pages to Excel and how we can generate PDF versions of our webpages on the fly.

See this content on www.pocketnerd.net -->

Sunday, June 26, 2005

PocketNerd Framework

The PocketNerd Framework provides additional classes and functionality that can be added to your applications

See this content on www.pocketnerd.net -->

Saturday, June 25, 2005

Xml Collection

Creates a collection of name value pair items that can be converted into an XML Document or transformed using XSLT. It is also possible to add groups of name value pair items.

See this content on www.pocketnerd.net -->

Grouped Hashtable

The GroupedHashtable is a class that looks very similar to a standard Hashtable except the string key is used to group the corresponding items together rather than identify a single, specific item.

See this content on www.pocketnerd.net -->

Tuesday, June 07, 2005

Name Value Pair

This is a simple class that is used to associate a Name with a corresponding Value and to add Items to ComboBoxes, ListBoxes etc

See this content on www.pocketnerd.net -->

Monday, June 06, 2005

High Performance Timers

There is no easy way to get very precise timings within the .NET Framework to see exactly how long code takes to execute or how long it takes to access a database. However, the Windows Platform does expose such functionality through performance counters and we can use interop to access it directly from within our .NET code

See this content on www.pocketnerd.net -->

Sunday, May 08, 2005

Convert a DataSet into a String

Sample code that shows how to how to convert an in memory DataSet into a String using a MemoryStream

See this content on www.pocketnerd.net -->

Saturday, April 16, 2005

PocketNerd WizardEngine

The PocketNerd Wizard Engine (Pwiz Engine) provides a powerful, simple, and consistent way to create Windows Forms based Wizards that can be launched as a modal or non-modal dialog from within your own Application, as a standalone executable that runs in its own process and as a Custom Wizard from within the Visual Studio.NET IDE.

This article describes how the Pwiz Engine works and how to create such Wizards.

See this content on www.pocketnerd.net -->

Wednesday, April 13, 2005

C# Custom Wizards

This is the final article in the series that looks at how more programming power can be introduced into our VS.NET Wizards by creating a C# Custom Wizard

Article 1: VS.NET Wizard Overview
Article 2: Custom HTML Wizards
Article 3: C# Custom Wizards


See this content on www.pocketnerd.net -->

Wednesday, April 06, 2005

IDTWizard.Execute's ContextParams

Sample code that shows how to make sense of the IDTWizard.Execute's ContextParams argument when creating a Custom Wizard

See this content on www.pocketnerd.net -->

Tuesday, February 22, 2005

VS.NET PropertyGrid

This article takes a look at the PropertyGrid Control in a Windows Forms application before moving on to using the VS.NET 2002/3/5 Properties Window from a hosted .NET UserControl

See this content on www.pocketnerd.net -->

Sunday, February 06, 2005

System Tray Application Wizard

This C# Wizard can be used to quickly create a new Windows Application that interacts with a System Tray icon

See this content on www.pocketnerd.net -->

.NET - COM Object Wizard

This C# Wizard creates a new .NET project and class that implements several best practices in exposing a COM object to a Win32 application. This is particularly useful for Excel and other Office .NET development activities

See this content on www.pocketnerd.net -->