如何以声明方式将 asp:listitem 设置为枚举值?

发布于 2024-09-05 12:04:20 字数 168 浏览 0 评论 0原文

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

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

我收到错误列表项不支持数据绑定。有什么想法吗?

I have an asp:RadioButtonList and want to declaratively bind the value to an enumeration. I tried using this type syntax:

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

I get an error list item does not support databinding. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-09-12 12:04:20

本质上,你无法完全按照自己的意愿去做。这是因为 Asp:Listitem 不包含数据绑定事件。然而,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

Essentially you cannot do exactly what you want to. This is because Asp:Listitem does not contain the Databinding event. The RadioButtonList itself does support this however.

So here is the closest I could come to what you wanted.

Here is the HTML

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

Here is the code behind

 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
心是晴朗的。 2024-09-12 12:04:20

我迭代枚举而不是绑定。

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

I iterate through the enum rather than binding.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文