Is this a bug in ASP.NET 3.5?-Collection of common programming errors

In ASP.NET when I try to add a dynamic control (includes validation) to Placeholder or any other control container, the name of control become an important. For example, this is very normal, easy control adding code.

var control = LoadControl("TestUserControl.ascx");
control.ID = Guid.NewGuid().ToString();
PlaceHolder1.Controls.Add(control);

as you see, I’m giving guid to control’s ID. In Runtime, this code fails, and compiler says this is a javascript error, and error message like ‘;’ character expected, missing.. etc..

The problem is very interesting. Dynamically added ASP.NET control (includes validation), causes an error because of ” – ” character in dynamically named ID property (or anything like ‘-‘, ‘.’,etc..).

When I refine my code like:

var control = LoadControl("TestUserControl.ascx");
control.ID = Guid.NewGuid().ToString().Replace("-", string.Empty);
PlaceHolder1.Controls.Add(control);

problem goes away 🙂

Is this a bug in ASP.NET 3.5? Why its look like a javascript error in page?