Tim Maxey .NET Technology Blog & Resources

Update to Highlighted CheckboxList .NET

First, throw this into a Mater Page, or the page you have your CheckBoxList control on...

<script type="text/javascript">
function uStyle(cb) {
cb.parentNode.style.backgroundColor =
cb.checked ? "yellow" : "" ;

cb.parentNode.style.color =
cb.checked ? "blue" : "" ;
}
</script>


Now I databind the CheckBoxList, so if you do not (you just loaded values manually) then take the the CheckBoxList1.DataBind reference out...

I have a Sub I call on the Page Load event: If Not Page.IsPostBack then... "AddScript"

Protected Sub AddScript()
CheckBoxList1.DataBind
        Dim MyItem As ListItem
        For Each MyItem In CheckBoxList1.Items
            MyItem.Attributes.Add("onclick", "uStyle(this);")
        Next
    End Sub

This seems to work a lot better than having to use an Update Panel and a ONSELECTED_CHANGE event on the list CheckBoxList Control...

Of course in .NET 3.5 you could/can leave out the whole Dim MyItem as ListItem...

For each item in CheckBoxlist1.Items would work too... :-)


by Tim Maxey, Life Coach, Internet GURU


Feedback

No comments posted yet.


Post a comment





 

Please add 6 and 4 and type the answer here: