[英]How to add enum properties to a WebControl
Edit: It looks like it might be a Visual Studio issue. 编辑:看起来可能是Visual Studio问题。 If I restart Visual Studio it works until I rebuild the solution.
如果我重新启动Visual Studio,则在重新生成解决方案之前它将一直有效。
I'm getting an "'B' could not be set on property 'MyMode'" exception in the designer when using this code: 使用以下代码时,在设计器中出现“无法在属性“ 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;
}
}
}
To reproduce: Create the control in a project. 重现:在项目中创建控件。 Drag the control onto a web form in another project.
将控件拖到另一个项目中的Web窗体上。 Set MyMode = B in the properties window.
在属性窗口中设置MyMode =B。 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{}
}
You're trying to set the value to 'B' which is a string. 您试图将值设置为字符串“ B”。 You need to set it to a numeric value, since that's what enums are....
您需要将其设置为数值,因为这就是枚举。
...
set
{
ViewState["MyMode"] = value; // <-- 'value' must be an integer equivalent to B
// in this example, to set as 'B', 'value' == 1
}
...
EDIT see this article 编辑看这篇文章
This is a Visual Studio 2008 SP1 bug 这是Visual Studio 2008 SP1错误
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361826 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 请注意,实际上有两个修补程序发布,如以下所述: 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和2009,而另一个用于Windows Vista和Windows Server 2008。
Windows XP and 2003: http://support.microsoft.com/kb/969612/ Windows XP和2003: http : //support.microsoft.com/kb/969612/
Windows Vista and Windows Server 2008: http://support.microsoft.com/kb/967535/ Windows Vista和Windows Server 2008: http : //support.microsoft.com/kb/967535/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.