开发者

Is this a bug in ASP.NET 3.5?

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), c开发者_如何学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?


It's not a bug so much as misuse. You couldn't give a control an ID that included a - if you did it within the IDE, so it makes sense that attempting to do it dynamically should have unintended results. The math functions are riddled with peculiarities like this that aren't bugs. Sure MS probably could have put in a function in the property that checked for invalid characters in the ID before allowing it to be set, but that would be a runtime error anyway (which is what you have).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜