Search code examples
asp.netvb.netdata-bindingradiobuttonlistitemdatabound

RadioButtonList 1 Row Coloured


Please Help.

I am using ASP/ VB.net and I have a radio button populated from the database in the code behind like this.

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

I now get the radio list on the screen and I want to colour 1 row, when I use this they all go the same colour.

rdoTheTest.ForeColor = Drawing.Color.Red

In the past when using a listview scenario I could do something like this;

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

My Intention was to do this;

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

Problem is only 1 row is retrieved in DataBinding when there is 10 rows and when using ListView I get 10 rows because you can use Handles ItemDataBound you can manipulate on every row pull.

Any ideas.


Solution

  • You can always color the specific radio after the data have been bound using OnDataBound, for example:

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

    UPDATE:

    Iterate bound list items and set the appropriate color based on the item value (or text):

        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