从枚举中加载radiobutton列表时,如何在每个单选按钮旁边显示文本?

StackOverflow https://stackoverflow.com/questions/811693

  •  03-07-2019
  •  | 
  •  

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

有帮助吗?

解决方案

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();
    }
}

给它一个旋转,看看它是否适合你。

干杯!

其他提示

您应该能够在控件上使用.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

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

我有一个小库,相同的代码等,为此目的提供了一个很好的界面,可以在枚举上使用属性。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top