我有一个asp:RadioButtonList,并希望以声明方式将值绑定到枚举。我尝试使用这种类型的语法:
value = <%# ((int)MyEnum.Value).ToString() %>"
我得到错误列表项不支持数据绑定。有什么想法吗?
答案 0 :(得分: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
答案 1 :(得分: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);
}