如何向 WebControl 添加枚举属性

发布于 2024-08-12 01:29:40 字数 808 浏览 10 评论 0原文

编辑:看起来可能是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-08-19 01:29:40

您试图将值设置为“B”,它是一个字符串。您需要将其设置为数字值,因为这就是枚举......

...
set
{
  ViewState["MyMode"] = value; // <-- 'value' must be an integer equivalent to B
  // in this example, to set as 'B', 'value' == 1
}
...

编辑请参阅这篇文章

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

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

喜你已久 2024-08-19 01:29:40

这是 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文