这是在ASP.NET 3.5中的错误? [英] Is this a bug in ASP.NET 3.5?

查看:129
本文介绍了这是在ASP.NET 3.5中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET当我尝试添加一个动态控制(包括验证)为占位符或任何其它控制容器控件的名称成为重要的。例如,这是非常正常的,容易控制添加code。

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);

你看,我给GUID来控制的ID。在运行时,此code失败,编译器说,这是一个JavaScript错误,而像错误信息;性格预期,失踪...等。

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

问题是很有趣的。动态添加ASP.NET控件(包括验证),会导致因错误 - 在动态命名ID属性字符。(或类似的东西' - ''。',等..)

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

当我改进我的code,如:

When I refine my code like:

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

问题消失:)

这是在 ASP.NET 3.5 的错误吗?为什么它看起来像在网页JavaScript错误?

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

推荐答案

这与其说是由于使用不当的错误。你不能给一个控件,包括ID的 - 如果您在IDE中做到了,所以这是有道理的,试图动态做它应该有意想不到的效果。数学功能都充斥着这样的不属于错误的特殊性。当然MS大概可以已经把在该检查在ID无效字符允许它进行设置前的属性功能,但是这将是一个运行时错误呢(这是你所拥有的)。

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

这篇关于这是在ASP.NET 3.5中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆