C# 如何将枚举属性添加到WebControl
编辑:看起来可能是Visual Studio的问题。如果我重新启动Visual Studio,它将一直工作,直到我重新生成解决方案为止 使用此代码时,我在设计器中遇到“无法在属性“MyMode”上设置“B”异常:C# 如何将枚举属性添加到WebControl,c#,asp.net,designer,visual-studio-2008-sp1,C#,Asp.net,Designer,Visual Studio 2008 Sp1,编辑:看起来可能是Visual Studio的问题。如果我重新启动Visual Studio,它将一直工作,直到我重新生成解决方案为止 使用此代码时,我在设计器中遇到“无法在属性“MyMode”上设置“B”异常: public class MyControl : CompositeControl { public enum MyEnum{ A, B } [DefaultValue(MyEnum.A)] public MyEnum MyMode { get {
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" />
您正在尝试将值设置为字符串“B”。您需要将其设置为一个数值,因为枚举就是这样的
...
set
{
ViewState["MyMode"] = value; // <-- 'value' must be an integer equivalent to B
// in this example, to set as 'B', 'value' == 1
}
...
。。。
设置
{
ViewState[“MyMode”]=value;//这是一个VisualStudio2008SP1错误
请注意,实际上发布了两个修补程序,如上所述:
一个用于Windows XP和2009,另一个用于Windows Vista和Windows Server 2008
Windows XP和2003:
Windows Vista和Windows Server 2008:
关于int情况的好主意。设置ViewState[“MyMode”]=1
,只是作为一个测试,并不能消除错误。您只是尝试了public MyEnum MyMode{get;set;}
?
...
set
{
ViewState["MyMode"] = value; // <-- 'value' must be an integer equivalent to B
// in this example, to set as 'B', 'value' == 1
}
...