Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

ASP.NET Controls – Validators Problem (NetFx 1.1 versus NetFx 2.0 or Higher)

0.00/5 (No votes)
15 Apr 2010CPOL3 min read 1  
ASP.NET controls - validators problem

It is not a secret that I have a passion related to ASP.NET Controls ID and all related subjects.

This time, I’ll write about a problem I found while migrating a NetFx 1.1 application to a new server.

The problem is about Validators and how they render the data needed to validate Client-Side.

Scenario NetFX 1.1.4

All data is rendered through attributes and this way, they must only follow the attributes naming conventions.

Scenario NetFX 2.0 or higher

Most required data is rendered through Expando attributes. This means that a JavaScript object is created to host the required data.

At first look, this seems quite the same, only with a different storage  approach, but it hides one tricky issue related to naming convention differences between HTML attributes and JavaScript variables.

The Tricky Issue

I believe that most of us try to follow the best patterns and naming conventions and if we all did this on old applications, this wouldn’t be a problem.

While migrating an old .Text v0.91 application to a new server, without NetFX 1.1 installed, I found that all pages with Validators raise a JavaScript error.

Digging into it, I found the error in this line:

var ctl00_pageBody-2_RequiredFieldValidator1 = 
document.all ? document.all["ctl00_pageBody-2_RequiredFieldValidator1"] : 
document.getElementById("ctl00_pageBody-2_RequiredFieldValidator1");

The problem is obvious, the character ‘-‘ is a JavaScript operator and cannot be used in a variable name . The JavaScript parser after finding the ‘-‘ say that an ‘;’ is expected after ‘ctl00_pageBody’.

So … why this didn’t occur in the old server? What led us to this point?

As I noticed before, in NetFX 1.1 this object didn’t exist, all data were simple rendered as attributes.

I didn’t find any good reason for naming a control like this but in fact this is a absolutely valid control ID.

Solutions

There are two approaches, we can either force the validator to render attributes or change the ID value to remove unwanted characters.

The first is obviously the better one but as you will see is a global application change.

Instruct the Validator to Render Attributes

The validator controls are ready to work in a legacy mode, but to enable that mode, we need to change all behavior of our application. We can do that adding the following web.config data:

XML
<system.web>
<xhtmlConformance mode="Legacy"/>

While this solution is the one I like the most, I choose to use the other simply because I don’t know exactly the potential side-effects, and I need to made a chirurgical change minimizing impacts.

Change the Controls ID

This approach requires you to change all controls ID in the validators hierarchy to prevent JavaScript naming problems. It could take some time but, usually, it should present no problems.

That is not my case. It took me some time to find where the pageBody-2 control.

I ended up finding that it was a UserControl, loaded dynamically and its ID was also composed dynamically. Unfortunately, this was all done in a private method belonging to an internal base class and the only practical solution was to override the Page Render method and replace the pattern ‘_pageBody-‘ with ‘_pageBody_’.

I know it is a dirty solution … I don’t like it … but this is a short term solution before migrating the application to CommunityServer.

Mental Note

Never use the ‘-‘ to name your controls ID property. Try the ‘_’ or any other without special meaning in JavaScript.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)