简体   繁体   中英

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? I keep receiving this error.

Cannot create an object of type 'MyEnum' from its string representation 'MyEnum.MyValue' for the 'MyProperty' property.

This is the code behind MyWebUserControl.ascx.cs

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

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. It's just there as a hint for VS to offer additional databinding properties in the property grid. It doesn't affect the ASP.NET runtime at all.

To set this property statically in the ASPX markup, just use the enum's value :

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

If you want to set it dynamically, you can do it in 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:

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

Note that the databinding option should be used only when 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.

This workaround might help solve the issue. You can tie the "real" property to a string property like so:

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. 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.
  • [System.ComponentModel.Bindable(true)] is NOT necessary on the property.

Example

MyWebUserControl.ascx.cs

public MyEnum MyProperty { get; set; }

SomeWebUserControl.ascx

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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