简体   繁体   English

如何将枚举值设置为Web用户控件的自定义属性?

[英]How to set an enum value to a custom property of a web user control?

How do we bind an enum value to the property of a web user control? 我们如何将枚举值绑定到Web用户控件的属性? I keep receiving this error. 我一直收到此错误。

Cannot create an object of type 'MyEnum' from its string representation 'MyEnum.MyValue' for the 'MyProperty' property. 无法从其字符串表示形式“ MyEper.MyValue”的“ MyProperty”属性创建“ MyEnum”类型的对象。

This is the code behind MyWebUserControl.ascx.cs 这是MyWebUserControl.ascx.cs背后的代码

[System.ComponentModel.Bindable(true)]
public MyEnum MyProperty { get; set; }

SomeWebUserControl.ascx SomeWebUserControl.ascx

<!-- This fails -->
<uc1:MyWebUserControl runat="server" ID="MyControl1"
    MyProperty="MyEnum.MyValue" />

<!-- This fails -->
<uc1:MyWebUserControl runat="server" ID="MyControl2" 
    MyProperty="<%# MyEnum.MyValue %>" />

<!-- This works -->
<uc1:MyWebUserControl runat="server" ID="MyControl3"
    MyProperty="0" />

Defining the property on the control is easy, as you have done: 定义控件上的属性很容易,就像您已经做的那样:

[System.ComponentModel.Bindable(true)]
public MyEnum MyProperty { get; set; }

However, it's worth noting that the [Bindable(true)] attribute isn't needed. 但是,值得注意的是,不需要[Bindable(true)]属性。 It's just there as a hint for VS to offer additional databinding properties in the property grid. 它只是为了提示VS在属性网格中提供其他数据绑定属性。 It doesn't affect the ASP.NET runtime at all. 它根本不影响ASP.NET运行时。

To set this property statically in the ASPX markup, just use the enum's value : 要在ASPX标记中静态设置此属性,只需使用枚举

<uc1:MyWebUserControl runat="server"
    ID="MyControl1"
    MyProperty="MyValue" />

If you want to set it dynamically, you can do it in Page_Init : 如果要动态设置,可以在Page_Init

void Page_Init() {
    MyControl1.MyProperty = GetEnumValueFromSomewhere();
}

<uc1:MyWebUserControl runat="server"
    ID="MyControl1" />

And finally, to set it dynamically as part of databinding, you can use the <%# foo %> databinding syntax: 最后,要将其动态设置为数据绑定的一部分,可以使用<%# foo %>数据绑定语法:

<uc1:MyWebUserControl runat="server"
    ID="MyControl1"
    MyProperty="<%# GetEnumValueFromSomewhere() %>" />

Note that the databinding option should be used only when databinding . 请注意, 只有在databinding时才应使用databinding选项。 If you're not trying to do databinding, it is rather wasteful to call DataBind() on the entire page, or even just on that one control. 如果您不尝试进行数据绑定,则在整个页面甚至仅在那个控件上调用DataBind()都是很浪费的。

This workaround might help solve the issue. 此解决方法可能有助于解决问题。 You can tie the "real" property to a string property like so: 您可以将“ real”属性绑定到字符串属性,如下所示:

private MyEnum _MyProperty = MyEnum.Default;

[System.ComponentModel.Bindable(true)]
public MyEnum MyProperty
{
    get {
        return _MyProperty;
    }
    set {
        _MyProperty = value;
    }
}

public string MyPropertyString 
{
    get {
        return _MyProperty.ToString();
    }
    set {
        _MyProperty = (MyEnum)Enum.Parse( typeof(MyEnum), value, true );
    }
}

And use 'MyPropertyString` on the control. 并在控件上使用“ MyPropertyString”。 It's not an elegant solution, but it may do the trick. 这不是一个优雅的解决方案,但可以解决问题。 Seems like there HAS to be a better answer than this, but I looked around and was not able to find anything yet. 似乎有比这更好的答案了,但是我环顾四周,却找不到任何东西。

Page.DataBind()

  • Page.DataBind() is necessary - that's what was missing. Page.DataBind()是必需的-这就是所缺少的。
  • [System.ComponentModel.Bindable(true)] is NOT necessary on the property. 该属性上不需要[System.ComponentModel.Bindable(true)]

Example

MyWebUserControl.ascx.cs MyWebUserControl.ascx.cs

public MyEnum MyProperty { get; set; }

SomeWebUserControl.ascx SomeWebUserControl.ascx

<uc1:MyWebUserControl runat="server" ID="MyControl" 
    MyProperty="<%# MyEnum.MyValue %>" />

SomeWebUserControl.ascx.cs SomeWebUserControl.ascx.cs

protected void Page_Load(object sender, EventArgs e)
{
    // this is 100% necessary to bind enum
    Page.DataBind();
}

See also: Render DateTime.Now directly in the ASPX page 另请参阅: 渲染DateTime.Now直接在ASPX页面中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM