ASP.NET 2.0 Client Validation Javascript Bug

0 comments suggest edit

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?

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

13 responses

  1. Avatar for Joshua Flanagan
    Joshua Flanagan July 14th, 2006

    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.

  2. Avatar for Scott
    Scott July 14th, 2006

    Good find. I'd post it on Connect (https://connect.microsoft.c....

  3. Avatar for Mike Swaim
    Mike Swaim July 17th, 2006

    Um, you posted it 4 times in a row to Connect.

  4. Avatar for Haacked
    Haacked July 17th, 2006

    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.

  5. Avatar for Adam
    Adam February 19th, 2007

    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?

  6. Avatar for Haacked
    Haacked February 19th, 2007

    Not sure about .NET 3.0. They marked it as something they won't resolve.

  7. Avatar for Hoa
    Hoa May 20th, 2007

    Excellent post! It resolved a lot of my headache. Keep up the good work :)

  8. Avatar for rschiefer
    rschiefer February 21st, 2008

    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.

  9. Avatar for Vera
    Vera April 7th, 2008

    The problem still exists in .NET 3. I have have '|' in id and I have the same problem

  10. Avatar for Neil Gibbons
    Neil Gibbons July 8th, 2008

    What an absolute life saver!
    God bless you Phil!

  11. Avatar for jayaseelan
    jayaseelan August 8th, 2008

    function Report_validation()
    {

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

    }

  12. Avatar for Joe
    Joe December 22nd, 2008

    why put special characters in the id in the first place? Come on!

  13. Avatar for The_Assimilator
    The_Assimilator January 30th, 2009

    @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.