4

我正在从枚举(垂直显示)加载单选按钮列表。我需要显示描述每个单选按钮选择的文本。我正在将它加载到代码隐藏中。

4

4 回答 4

4

我最近发现 Enum 类有很多方面的用途越来越多,其中之一就是 GetNames 方法。此方法返回指定枚举中所有名称的字符串数组。

此代码假定您的页面上有一个名为RadioButtonList1 的RadioButtonList

public enum AutomotiveTypes
{
    Car,
    Truck,
    Van,
    Train,
    Plane
}

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] automotiveTypeNames = Enum.GetNames(typeof(AutomotiveTypes));

        RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;

        RadioButtonList1.DataSource = automotiveTypeNames;
        RadioButtonList1.DataBind();
    }
}

试一试,看看它是否对你有用。

干杯!

于 2009-05-01T15:19:00.573 回答
1

您应该能够在控件上使用 .Text 属性。

http://www.w3schools.com/ASPNET/control_radiobutton.asp

编辑:

实际上我认为错过了这个问题,我相信这就是你要找的

For Each val As [Enum] In [Enum].GetValues(GetType(YourEnum))
        Radio Button Add Logic Here
Next
于 2009-05-01T14:50:23.403 回答
0

有几个想法是要么拥有一个将描述字符串映射到枚举值的字典,要么你可以用一个属性来装饰你的枚举值

于 2009-05-01T14:54:02.630 回答
0

我有一个小库,相同的代码等,它提供了一个很好的接口,用于为此目的在枚举上使用属性。

http://daniel-mcadam.com/blog/2009/11/05/User-Display-Text-For-Enums.aspx

于 2009-11-14T09:40:48.363 回答