"无法通过表达式引用类型"使用枚举值时

时间:2015-08-18 16:19:59

标签: c# asp.net enums webforms

我在尝试通过html标记设置用户控件初始化时收到错误cannot reference a type through an expression。我可能正在拍摄一些我甚至无法在这里实现的东西,但我能找到的唯一帮助是与视频游戏和MVC有关,但事实并非如此。

基本上,我想通过html中的标记本身来初始化类型为enum的用户控件值。因此,当您声明用户控件时,它会设置所需的所有值。似乎只是抛出那个错误,任何见解?

标记

<uc:ucStateDropDownList id="UserControls_ucStateDropDownList" runat="server" StatesDisplayed="All"/>

背后的守则

public partial class UserControls_ucStateDropDownList : System.Web.UI.UserControl
{
    private StatesDisplayedInControl m_statesToDisplay;
    /// <summary>
    /// Existing options are as follows:
    ///     - UnitedStates (US states only)
    ///     - Canada (CDN states only)
    ///     - All (All states available)
    /// Default setting is 'All'.   
    /// </summary>
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public StatesDisplayedInControl StatesDisplayed 
    {
        get { return m_statesToDisplay; }
        set { m_statesToDisplay = value; }
    }
    public enum StatesDisplayedInControl : int
    {
        UnitedStates = 0,
        Canada = 1,
        All = 2
    }
}

1 个答案:

答案 0 :(得分:2)

当定义为类的一部分时,ASP.NET似乎无法识别枚举。只需在自己的文件中定义它,或重新排列如下,以使其正常工作:

public partial class UserControls_ucStateDropDownList : System.Web.UI.UserControl
{
    private StatesDisplayedInControl m_statesToDisplay;
    /// <summary>
    /// Existing options are as follows:
    ///     - UnitedStates (US states only)
    ///     - Canada (CDN states only)
    ///     - All (All states available)
    /// Default setting is 'All'.   
    /// </summary>
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public StatesDisplayedInControl StatesDisplayed 
    {
        get { return m_statesToDisplay; }
        set { m_statesToDisplay = value; }
    }
}

public enum StatesDisplayedInControl : int
{
    UnitedStates = 0,
    Canada = 1,
    All = 2
}