gpt4 book ai didi

asp.net - 如何设置asp :listitem to an enumeration value declaratively?

转载 作者:行者123 更新时间:2023-12-02 04:15:30 34 4
gpt4 key购买 nike

我有一个 asp:RadioButtonList 并希望以声明方式将值绑定(bind)到枚举。我尝试使用这种类型的语法:
value = <%# ((int)MyEnum.Value).ToString() %>"
我得到一个错误列表项不支持数据绑定(bind)。有任何想法吗?

最佳答案

本质上,您不能完全按照您的意愿去做。这是因为 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

关于asp.net - 如何设置asp :listitem to an enumeration value declaratively?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3025065/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com