如何向 WebControl 添加枚举属性
编辑:看起来可能是 Visual Studio 问题。如果我重新启动 Visual Studio,它会一直工作,直到我重建解决方案。
使用此代码时,我在设计器中遇到“无法在属性“MyMode”上设置“B””异常:
public class MyControl : CompositeControl
{
public enum MyEnum{ A, B }
[DefaultValue(MyEnum.A)]
public MyEnum MyMode
{
get
{
object obj = ViewState["MyMode"];
return obj == null ? MyMode.A : (MyEnum)obj;
}
set
{
ViewState["MyMode"] = value;
}
}
}
重现:在项目中创建控件。将控件拖到另一个项目中的 Web 窗体上。在属性窗口中设置 MyMode = B。关闭窗体,重新打开设计器。
我做错了什么?我需要手动将字符串解析为枚举吗?
编辑:设计器生成的代码。
<cc1:MyControl ID="MyControl1" runat="server" MyMode="B" />
编辑:事实上,这个属性也失败了:
public MyEnum MyMode
{
get
{
return MyEnum.A;
}
set{}
}
Edit: It looks like it might be a Visual Studio issue. If I restart Visual Studio it works until I rebuild the solution.
I'm getting an "'B' could not be set on property 'MyMode'" exception in the designer when using this code:
public class MyControl : CompositeControl
{
public enum MyEnum{ A, B }
[DefaultValue(MyEnum.A)]
public MyEnum MyMode
{
get
{
object obj = ViewState["MyMode"];
return obj == null ? MyMode.A : (MyEnum)obj;
}
set
{
ViewState["MyMode"] = value;
}
}
}
To reproduce: Create the control in a project. Drag the control onto a web form in another project. Set MyMode = B in the properties window. Close the form, reopen the designer.
What am I doing wrong? Do I need to manually parse the string into an enum?
Edit: Designer generated code.
<cc1:MyControl ID="MyControl1" runat="server" MyMode="B" />
Edit: In fact, this property also fails:
public MyEnum MyMode
{
get
{
return MyEnum.A;
}
set{}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图将值设置为“B”,它是一个字符串。您需要将其设置为数字值,因为这就是枚举......
编辑请参阅这篇文章
You're trying to set the value to 'B' which is a string. You need to set it to a numeric value, since that's what enums are....
EDIT see this article
这是 Visual Studio 2008 SP1 错误
https://connect.microsoft。 com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361826
请注意,实际上已经发布了两个修补程序,如下所述:http://support.microsoft.com/kb/961847
一个适用于 Windows XP 和 2009,另一个适用于 Windows Vista 和 Windows Server 2008。
Windows XP 和 2003:
http://support.microsoft.com/kb/969612/
Windows Vista 和 Windows Server 2008年:
http://support.microsoft.com/kb/967535/
This is a Visual Studio 2008 SP1 bug
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361826
Please note that there are actually two hotfixes released, as described on: http://support.microsoft.com/kb/961847
One is for Windows XP and 2009, while the other is for Windows Vista and Windows Server 2008.
Windows XP and 2003:
http://support.microsoft.com/kb/969612/
Windows Vista and Windows Server 2008:
http://support.microsoft.com/kb/967535/