RadioButtonList 1行着色

时间:2018-04-30 13:55:49

标签: asp.net vb.net data-binding radiobuttonlist itemdatabound

请帮助。

我正在使用ASP / VB.net,我在后面的代码中从数据库填充了一个单选按钮。

rdoTheTest.DataSource = Test.Test_Get()
rdoTheTest.DataBind()

我现在在屏幕上看到收音机列表,我想要为1行着色,当我使用它时,它们都会变成相同的颜色。

rdoTheTest.ForeColor = Drawing.Color.Red

在过去使用listview场景时,我可以做这样的事情;

Private Sub LvTheTest_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles lvTheTest.ItemDataBound
    Dim SomeTextBox As TextBox = DirectCast(e.Item.FindControl("SomeTextBox"), TextBox)
    If DataBinder.Eval(e.Item.DataItem, "SomeDBVAR") = 1 Then SomeTextBox.ForeColor = Drawing.Color.Red
End Sub

我的意图是这样做;

Private Sub rdoTheTest_DataBinding(sender As Object, e As EventArgs) Handles rdoTheTest.DataBinding
    If DataBinder.Eval(e.Item.DataItem, "SomeDBVAR") = 1 Then sender.ForeColor = Drawing.Color.Red
End Sub

问题是,当有10行时,在DataBinding中只检索到1行,当使用ListView时,我得到10行,因为你可以使用Handles ItemDataBound,你可以在每一行上操作。

任何想法。

1 个答案:

答案 0 :(得分:1)

使用OnDataBound 绑定数据后,您始终可以为特定的电台着色,例如:

Protected Sub rdoTheTest_DataBound(sender As Object, e As EventArgs)
    rdoTheTest.Items.FindByValue("SomeDBVar").Attributes.Add("style", "color: red")
End Sub

<强>更新

迭代绑定列表项并根据项值(或文本)设置适当的颜色:

    For Each item As ListItem In rdoTheTest.Items
        If item.Value = "1" Then
            item.Attributes.Add("style", "color: red")
        Else If item.Value = "2" Then
            item.Attributes.Add("style", "color: orange")
        Else 
            item.Attributes.Add("style", "color: green")
        End If
    Next