4

我有一个 asp:RadioButtonList 并希望以声明方式将值绑定到枚举。我尝试使用这种类型的语法:

value = <%# ((int)MyEnum.Value).ToString() %>"

我得到一个错误列表项不支持数据绑定。有任何想法吗?

4

2 回答 2

1

本质上,您不能完全按照您的意愿去做。这是因为 Asp:Listitem 不包含 Databinding 事件。RadioButtonList 本身确实支持这一点。

所以这是我最接近你想要的东西。

这是HTML

<asp:RadioButtonList runat="server" ID="rbl" DataSource='<%# EnumValues %>' DataValueField='Value'  DataTextField='Key' />

这是后面的代码

 Public Enum values As Integer
    first = 1
    second = 2
    third = 3
    fourth = 4
    fifth = 5

End Enum

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Page.DataBind()
End Sub

Public ReadOnly Property EnumValues() As System.Collections.Generic.Dictionary(Of String, String)
    Get


        Dim val As values

        Dim names As Array
        Dim values As Array


        Dim stuff As Dictionary(Of String, String) = New Dictionary(Of String, String)

        names = val.GetNames(val.GetType)
        values = val.GetValues(val.GetType)

        build the final results
        For i As Integer = 0 To names.Length - 1
            stuff.Add(names(i), values(i))
        Next

        Return stuff

    End Get
End Property
于 2010-06-14T18:46:12.667 回答
0

我遍历枚举而不是绑定。

Array itemValues = System.Enum.GetValues(typeof(Response));
Array itemNames = System.Enum.GetNames(typeof(Response));

for (int i = 0; i <= itemNames.Length - 1 ; i++) {
    ListItem item = new ListItem(itemNames(i), itemValues(i));
    radioButtonList1.Items.Add(item);
}
于 2010-06-11T18:42:42.147 回答