ASP.NET 2.0 Client Validation Javascript Bug

Bug I recently ran into a perplexing problem that I believe is a bug in ASP.NET 2.0.

Subtext dynamically loads UserControls into the page when fulfilling a request. When commenting on a post, we load a user control that contains the comment form fields and some instances of the RequiredFieldValidator validation control.

While testing, I noticed I kept getting javascript errors when trying to post a comment. Here is the error message:

missing ; before statement

Viewing the source, I noticed the error occurs in the javascript generated by the ASP.NET runtime for client side validation. Here is a tiny snippet of the line with the problem.

var ControlWithValidators.ascx_validateThat = document.all ? ...

Notice the problem? There is a dot in the variable name that Javascript does not like since ControlWithValidators is not an object. What the?

After some digging around I found the culprit. I won’t bore you with the nitty gritty details. When dynamically adding controls to a page, it is a good idea to specify an id before adding them to the Controls collection. That way the controls can reload their state on Postback. However the snippet of code I found was giving the controls an ID that contained a period.

To prove this was indeed the culprit, I created a new simple VS.NET 2005 Web Application Project that exhibits the bug. The page dynamically loads a user control that contains a validation control. Here is the Page_Load method of the web page. When you compile this and run the page, you will see the javascript error.

protected void Page_Load(object sender, EventArgs e)
{
    Control control = LoadControl("ControlWithValidators.ascx");
    control.ID = "ControlWithValidators.ascx";
    placeholder.Controls.Add(control);
}

The quick fix was to simply replace the period with an underscore when assigning the id. Hopefully this helps you if you ever run into something so obscure.

If you are interested in duplicating the bug, you can download the validator bug demo solution here. By the way, does anyone know where is the best place to report this kind of thing?

What others have said

Requesting Gravatar... Joshua Flanagan Jul 14, 2006 8:04 PM
# re: ASP.NET 2.0 Client Validation Javascript Bug
Nice one.
Post it to the Visual Studio & .NET Framework Feedback center of http://connect.microsoft.com
(formerly "ladybug")
Apparently your submission will be created as a work item in the internal product bug database.
Requesting Gravatar... Scott Jul 14, 2006 8:18 PM
# re: ASP.NET 2.0 Client Validation Javascript Bug
Good find. I'd post it on Connect (https://connect.microsoft.com/VisualStudio).
Requesting Gravatar... Mike Swaim Jul 18, 2006 7:25 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
Um, you posted it 4 times in a row to Connect.
Requesting Gravatar... Haacked Jul 18, 2006 8:44 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
That's because I experienced a problem with Connect. I was running Firefox as an LUA and it kept giving me an error message about trying to upload the file. I didn't realize that it had posted though.
Requesting Gravatar... Adam Feb 20, 2007 8:28 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
I have been following the connect site on this issue and from what I can tell it doesn't look like Microsoft had resolved it. Do you have any idea if the issue still exists in .NET 3?
Requesting Gravatar... Haacked Feb 20, 2007 10:49 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
Not sure about .NET 3.0. They marked it as something they won't resolve.
Requesting Gravatar... Hoa May 21, 2007 2:05 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
Excellent post! It resolved a lot of my headache. Keep up the good work :)
Requesting Gravatar... rschiefer Feb 21, 2008 12:33 PM
# re: ASP.NET 2.0 Client Validation Javascript Bug
This problem is also caused by spaces in the id. I used .Replace(' ', '_') to convert spaces to underscores and that fixed the issue for me.
Requesting Gravatar... Vera Apr 08, 2008 6:41 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
The problem still exists in .NET 3. I have have '|' in id and I have the same problem
Requesting Gravatar... Neil Gibbons Jul 09, 2008 3:53 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
What an absolute life saver!
God bless you Phil!
Requesting Gravatar... jayaseelan Aug 09, 2008 5:16 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
function Report_validation()
{

var doc=document.forms[0].value;
if(doc.ddlReportType=="--Select--")
{
alert("Wrong");
}

}
Requesting Gravatar... Joe Dec 22, 2008 11:46 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
why put special characters in the id in the first place? Come on!
Requesting Gravatar... The_Assimilator Jan 31, 2009 5:09 AM
# re: ASP.NET 2.0 Client Validation Javascript Bug
@Joe: the HTML spec explicitly allows almost any character to be used in identifier names, although I think the pipe character "|" is invalid.

The problem is that Microsoft's code should strip out/replace JavaScript identifiers (., -, etc.) when converting element IDs to variable names, but it doesn't. This is a bug.

What do you have to say?

(will show your gravatar)
Please add 5 and 1 and type the answer here: